﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Hangjing.Model;
using Hangjing.Common;
using Hangjing.DBUtility;
using System.Data;
using System.Collections;
using System.Data.SqlClient;

namespace Hangjing.SQLServerDAL
{
    public class Custorder
    {

        /// <summary>
        /// 订单取消时，如果商家有支付要返回余额
        /// </summary>
        /// <param name="orerid"></param>
        /// <returns></returns>
        public int OrderCancelRefundForShop(string orderid)
        {
            TogoAddMoneyLogInfo model = new TogoAddMoneyLog().GetModel(orderid);
            if (model != null && model.Inve1 == 0)
            {
                model.AddMoney = Math.Abs(model.AddMoney);
                model.Inve1 = 1;

                string sql = "UPDATE TogoAddMoneyLog SET Inve1=1,AddDate = GETDATE() WHERE Inve2 = '" + orderid+"';UPDATE dbo.Points SET money = money + "+model.AddMoney+" WHERE Unid = "+model.UserId;
                Hangjing.DBUtility.SQLHelper.excutesql(sql);
            }


            return 1;
        }

        /// <summary>
        /// 返回配送员当日，当月完成订单数
        /// </summary>
        public ROrderinfo Deliverordercountsum(int deliverid)
        {
            SqlParameter[] Para =
            {
                new SqlParameter("@deliverid",SqlDbType.Int),
                new SqlParameter("@dayordercount",SqlDbType.Int,4),
                new SqlParameter("@mounthordercount",SqlDbType.Int,4),
            };
            Para[0].Value = deliverid;
            Para[1].Direction = ParameterDirection.Output;
            Para[2].Direction = ParameterDirection.Output;

            SQLHelper.ExecuteNonQuery(CommandType.StoredProcedure, "deliver_ordercountsum", Para);

            ROrderinfo rs = new ROrderinfo();
            rs.groupid = HJConvert.ToInt32(Para[1].Value.ToString());
            rs.cityid = HJConvert.ToInt32(Para[2].Value.ToString());

            return rs;
        }


        /// <summary>
        /// 按天查询
        /// </summary>
        /// <param name="where"></param>
        /// <returns></returns>
        public IList<CustorderInfo> DeliverProcessOrders(int deliverid)
        {
            IList<CustorderInfo> list = new List<CustorderInfo>();

            SqlParameter[] Para =
           {
                new SqlParameter("@deliver",SqlDbType.Int,5),
            };
            Para[0].Value = deliverid;

            using (SqlDataReader dr = SQLHelper.ExecuteReader(CommandType.StoredProcedure, "Custorder_DeliverProcessOrders", Para))
            {
                while (dr.Read())
                {
                    CustorderInfo model = new CustorderInfo();
                    model.sendstate = HJConvert.ToInt32(dr["sendstate"]);
                    list.Add(model);
                }
            }
            return list;
        }



        /// <summary>
        /// 根据条件查询下单用户的手机号用来发短信
        /// </summary>
        /// <param name="where"></param>
        /// <returns></returns>
        public string searchPhone(string where)
        {
            SqlParameter[] Para =
            {
                new SqlParameter("@where",SqlDbType.VarChar,1500),
            };
            Para[0].Value = where;

            string phones = "";


            using (SqlDataReader dr = SQLHelper.ExecuteReader(CommandType.StoredProcedure, "Custorder_searchPhone", Para))
            {
                if (dr.Read())
                {
                    phones = HJConvert.ToString(dr["phones"]);
                }
            }
            return phones;
        }

        /// <summary>
        /// 设置这个订单配送费提成基数（配送员收的）
        /// </summary>
        /// <param name="orderid"></param>
        /// <returns></returns>
        public decimal curDeliverGetSendFee(PointsInfo shop, CustorderInfo order, out decimal fee)
        {
            decimal DeliverGetSendFee_base = 0;
            if (order.delivergetfee_base == 0)
            {
                switch (shop.EBuilding)
                {
                    case "0":
                        DeliverGetSendFee_base = order.OrderSums * Convert.ToDecimal((Convert.ToDouble(shop.SN2) / 100));
                        break;
                    case "1":
                        DeliverGetSendFee_base = Convert.ToDecimal(shop.SN2);
                        break;
                    case "2":

                        deliverexpressFeeConfigInfo set = new deliverexpressFeeConfig().GetModel(shop.Unid);
                        if (set == null)
                        {
                            break;
                        }

                        //，1判断距离，2判断时间
                        decimal senconddistance = Math.Max(Math.Min((order.cardpay - set.basedistance), (set.seconddistance - set.basedistance)), 0);
                        decimal thireddistance = Math.Max((order.cardpay - set.seconddistance), 0);
                        DeliverGetSendFee_base = set.basedistanceprice + senconddistance * set.seconddistancePerPrice + thireddistance * set.thirdDistancePerPrice;

                        int sendtime = Convert.ToInt32(order.SendTime.ToShortTimeString().Replace(":", ""));

                        int nigone = Convert.ToInt32(set.starttime1.ToShortTimeString().Replace(":", ""));
                        int nigtwo = Convert.ToInt32(set.endtime1.Replace(":", ""));

                        if (nigone >= nigtwo)//说明跨天了。
                        {
                            if ((sendtime >= nigone && sendtime <= 2400) || (sendtime <= nigtwo))
                            {
                                DeliverGetSendFee_base += set.reveint1;
                            }
                        }
                        else
                        {
                            if (sendtime >= nigone && sendtime <= nigtwo)
                            {
                                DeliverGetSendFee_base += set.reveint1;
                            }
                        }


                        int nigthree = Convert.ToInt32(set.starttime2.ToShortTimeString().Replace(":", ""));
                        int nigfour = Convert.ToInt32(set.endtime2.Replace(":", ""));

                        if (nigthree >= nigfour)//说明跨天了。
                        {
                            if ((sendtime >= nigthree && sendtime <= 2400) || (sendtime <= nigfour))
                            {
                                DeliverGetSendFee_base += set.reveint2;
                            }
                        }
                        else
                        {
                            if (sendtime >= nigthree && sendtime <= nigfour)
                            {
                                DeliverGetSendFee_base += set.reveint2;
                            }
                        }

                        DeliverGetSendFee_base = Convert.ToInt32(DeliverGetSendFee_base + 0.0001M);


                        break;
                }
            }
            else
            {
                DeliverGetSendFee_base = order.delivergetfee_base;
            }

            //新的规则只是基数，收入=基数*比例
            if (Convert.ToInt32(shop.MgrCell.ToString()) > 0) //商家比例，不为0，就按商家
            {
                fee = DeliverGetSendFee_base * Convert.ToDecimal(shop.MgrCell) / 100;
            }
            else
            {
                fee = 0;// 使用配送员的比例时，这里直接是0
            }


            return Convert.ToInt32(DeliverGetSendFee_base);
        }





        /// <summary>
        /// 设置这个订单配送费提成（配送员收的）（跑腿订单）
        /// </summary>
        /// <param name="orderid"></param>
        /// <returns></returns>
        public decimal curDeliverGetSendFee(int cityid, CustorderInfo order)
        {
            decimal DeliverGetSendFee_base = 0;


            citydeliverexpressFeeConfigInfo set = new citydeliverexpressFeeConfig().GetModel(cityid);
            if (set == null)
            {
                return DeliverGetSendFee_base;
            }

            //，1判断距离，2判断时间
            decimal senconddistance = Math.Max(Math.Min((order.cardpay - set.basedistance), (set.seconddistance - set.basedistance)), 0);
            decimal thireddistance = Math.Max((order.cardpay - set.seconddistance), 0);
            DeliverGetSendFee_base = set.basedistanceprice + senconddistance * set.seconddistancePerPrice + thireddistance * set.thirdDistancePerPrice;

            Hangjing.AppLog.AppLog.Info("SendTime:" + order.SendTime.ToShortTimeString());

            int sendtime = Convert.ToInt32(order.SendTime.ToShortTimeString().Replace(":", ""));

            int nigone = Convert.ToInt32(set.starttime1.ToShortTimeString().Replace(":", ""));
            int nigtwo = Convert.ToInt32(set.endtime1.Replace(":", ""));

            if (nigone >= nigtwo)//说明跨天了。
            {
                if ((sendtime >= nigone && sendtime <= 2400) || (sendtime <= nigtwo))
                {
                    DeliverGetSendFee_base += set.reveint1;
                }
            }
            else
            {
                if (sendtime >= nigone && sendtime <= nigtwo)
                {
                    DeliverGetSendFee_base += set.reveint1;
                }
            }


            int nigthree = Convert.ToInt32(set.starttime2.ToShortTimeString().Replace(":", ""));
            int nigfour = Convert.ToInt32(set.endtime2.Replace(":", ""));

            if (nigthree >= nigfour)//说明跨天了。
            {
                if ((sendtime >= nigthree && sendtime <= 2400) || (sendtime <= nigfour))
                {
                    DeliverGetSendFee_base += set.reveint2;
                }
            }
            else
            {
                if (sendtime >= nigthree && sendtime <= nigfour)
                {
                    DeliverGetSendFee_base += set.reveint2;
                }
            }


            return Convert.ToInt32(DeliverGetSendFee_base);
        }


        /// <summary>
        /// 设置这个订单配送费提成（配送员收的）
        /// </summary>
        /// <param name="orderid"></param>
        /// <returns></returns>
        public int curDeliverGetSendFee(string orderid)
        {
            SqlParameter[] Para =
            {
                new SqlParameter("@orderid",SqlDbType.VarChar,20)
            };
            Para[0].Value = orderid;

            SQLHelper.ExecuteNonQuery(CommandType.StoredProcedure, "Custorder_curDeliverGetSendFee", Para);

            return 1;
        }

        /// <summary>
        /// 手动完成订单
        /// </summary>
        /// <param name="orderid"></param>
        /// <param name="did"></param>
        /// <returns></returns>
        public int manComplete(string orderid, int did)
        {
            SqlParameter[] Para =
            {
                new SqlParameter("@orderid",SqlDbType.VarChar,20),
                new SqlParameter("@did",SqlDbType.Int,5),
            };
            Para[0].Value = orderid;
            Para[1].Value = did;

            SQLHelper.ExecuteNonQuery(CommandType.StoredProcedure, "Custorder_manComplet", Para);
            if (did > 0)
            {
                new Deliver().InitWorkState(did);
            }


            return 1;
        }


        /// <summary>
        /// 按天查询
        /// </summary>
        /// <param name="where"></param>
        /// <returns></returns>
        public IList<CustorderInfo> GetSumGroupByDay(string where)
        {
            IList<CustorderInfo> list = new List<CustorderInfo>();

            string sql = "select sum(OrderSums) as  OrderTotal,Count(*) as OrderCount,sum(shopdiscountmoney) as shopdiscountmoney,sum(Advance) as Advance,sum(cardpay) as cardpay,sum (case when deliversiteid =0  then (shopdiscountmoney) else 0   end)  as nopaymoeny,sum (case when OrderSums >Advance and Advance > 0  then (OrderSums - Advance) else 0   end)  as profit";
            sql += " , sum (case when settlecycle = 1 or systemuserid=1   then(0) else shopdiscountmoney   end)  as delivergivemoney";
            sql += " , sum (case when  systemuserid=1  then(1) else 0   end)  as SystemUserId,CONVERT(varchar(100), orderdateTime, 23) AS mydate";
            sql += " from  (  Custorder union all select *  from eleCustorder )  Custorder  where " + where + " GROUP BY CONVERT(varchar(100), orderdateTime, 23) order by  mydate asc ";

            HJlog.toLog(sql);

            using (SqlDataReader dr = SQLHelper.ExecuteReader(CommandType.Text, sql, null))
            {
                while (dr.Read())
                {
                    CustorderInfo model = new CustorderInfo();
                    model.OrderCount = HJConvert.ToInt32(dr["OrderCount"]);
                    model.OrderTotal = HJConvert.ToDecimal(dr["OrderTotal"]);
                    model.shopdiscountmoney = HJConvert.ToDecimal(dr["shopdiscountmoney"]);
                    model.cardpay = HJConvert.ToDecimal(dr["cardpay"]);
                    model.Advance = HJConvert.ToDecimal(dr["Advance"]);
                    model.CallPhoneNo = HJConvert.ToDecimal(dr["nopaymoeny"]).ToString();
                    model.OldPrice = HJConvert.ToDecimal(dr["profit"]);
                    model.delivergivemoney = HJConvert.ToDecimal(dr["delivergivemoney"]);
                    model.SystemUserId = HJConvert.ToInt32(dr["SystemUserId"]);

                    model.tempcode = HJConvert.ToString(dr["mydate"]);

                    list.Add(model);
                }
            }
            return list;
        }

        /// <summary>
        /// 按天查询(包含ele的订单)
        /// </summary>
        /// <param name="where"></param>
        /// <returns></returns>
        public IList<CustorderInfo> GetSumGroupByDayWithEle(string where)
        {
            IList<CustorderInfo> list = new List<CustorderInfo>();


            SqlParameter[] Para =
            {
                new SqlParameter("@where",SqlDbType.VarChar,4000),
                new SqlParameter("@elewhere",SqlDbType.VarChar,4000),
            };
            Para[0].Value = where;
            Para[1].Value = where.Replace("Custorder.", "eleCustorder.");


            using (SqlDataReader dr = SQLHelper.ExecuteReader(CommandType.StoredProcedure, "cutorder_GetSumGroupByDay", Para))
            {
                while (dr.Read())
                {
                    CustorderInfo model = new CustorderInfo();
                    model.OrderCount = HJConvert.ToInt32(dr["OrderCount"]);
                    model.OrderTotal = HJConvert.ToDecimal(dr["OrderTotal"]);
                    model.shopdiscountmoney = HJConvert.ToDecimal(dr["shopdiscountmoney"]);
                    model.cardpay = HJConvert.ToDecimal(dr["cardpay"]);
                    model.Advance = HJConvert.ToDecimal(dr["Advance"]);
                    model.CallPhoneNo = HJConvert.ToDecimal(dr["nopaymoeny"]).ToString();
                    model.OldPrice = HJConvert.ToDecimal(dr["profit"]);
                    model.delivergivemoney = HJConvert.ToDecimal(dr["delivergivemoney"]);
                    model.SystemUserId = HJConvert.ToInt32(dr["SystemUserId"]);

                    model.tempcode = HJConvert.ToString(dr["mydate"]);

                    list.Add(model);
                }
            }
            return list;
        }



        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Hangjing.Model.CustorderInfo model)
        {
            SqlParameter[] parameters =
            {
                new SqlParameter("@InUse", SqlDbType.VarChar,50) ,
                new SqlParameter("@OrderDateTime", SqlDbType.DateTime) ,
                new SqlParameter("@OrderChecker", SqlDbType.Int,4) ,
                new SqlParameter("@OrderStatus", SqlDbType.Int,4) ,
                new SqlParameter("@OrderRcver", SqlDbType.VarChar,50) ,
                new SqlParameter("@OrderComm", SqlDbType.VarChar,50) ,
                new SqlParameter("@OrderAddress", SqlDbType.VarChar,50) ,
                new SqlParameter("@AddressText", SqlDbType.VarChar,100) ,
                new SqlParameter("@OrderAddrEx", SqlDbType.VarChar,300) ,
                new SqlParameter("@OrderAttach", SqlDbType.VarChar,200) ,
                new SqlParameter("@OrderSums", SqlDbType.Decimal,5) ,
                new SqlParameter("@Sender", SqlDbType.VarChar,256) ,
                new SqlParameter("@SendTime", SqlDbType.DateTime) ,
                new SqlParameter("@CallPhoneNo", SqlDbType.VarChar,256) ,
                new SqlParameter("@P2Sign", SqlDbType.VarChar,256) ,
                new SqlParameter("@SendFee", SqlDbType.Decimal,5) ,
                new SqlParameter("@paymode", SqlDbType.Int,4) ,
                new SqlParameter("@paytime", SqlDbType.DateTime) ,
                new SqlParameter("@paymoney", SqlDbType.Decimal,5) ,
                new SqlParameter("@paystate", SqlDbType.Int,4) ,
                new SqlParameter("@SetStateTime", SqlDbType.DateTime) ,
                new SqlParameter("@UserId", SqlDbType.Int,4) ,
                new SqlParameter("@TogoId", SqlDbType.Int,4) ,
                new SqlParameter("@orderid", SqlDbType.VarChar,50) ,
                new SqlParameter("@fromweb", SqlDbType.VarChar,50) ,
                new SqlParameter("@oldstatus", SqlDbType.Int,4) ,
                new SqlParameter("@systemuserid", SqlDbType.Int,4) ,
                new SqlParameter("@Commentstate", SqlDbType.Int,4) ,
                new SqlParameter("@writer", SqlDbType.VarChar,50) ,
                new SqlParameter("@IsShopSet", SqlDbType.Int,4) ,
                new SqlParameter("@CityID", SqlDbType.Int,4) ,
                new SqlParameter("@deliversiteid", SqlDbType.Int,4) ,
                new SqlParameter("@deliverheaderid", SqlDbType.Int,4) ,
                new SqlParameter("@deliverid", SqlDbType.Int,4) ,
                new SqlParameter("@sendstate", SqlDbType.Int,4) ,
                new SqlParameter("@ReveInt1", SqlDbType.Int,4) ,
                new SqlParameter("@ReveInt2", SqlDbType.Int,4) ,
                new SqlParameter("@ReveVar1", SqlDbType.VarChar,256) ,
                new SqlParameter("@ReveVar2", SqlDbType.VarChar,256) ,
                new SqlParameter("@ReveDate1", SqlDbType.DateTime) ,
                new SqlParameter("@ReveDate2", SqlDbType.DateTime) ,
                new SqlParameter("@isaddpoint", SqlDbType.Int,4) ,
                new SqlParameter("@OldPrice", SqlDbType.Decimal,5) ,
                new SqlParameter("@shopdiscountmoney", SqlDbType.Decimal,5) ,
                new SqlParameter("@tempcode", SqlDbType.VarChar,256) ,
                new SqlParameter("@cardpay", SqlDbType.Decimal,5) ,
                new SqlParameter("@cardids", SqlDbType.VarChar,256) ,
                new SqlParameter("@pointrat", SqlDbType.Int,4) ,
                new SqlParameter("@Packagefee", SqlDbType.Decimal,9) ,
                new SqlParameter("@settleuser", SqlDbType.VarChar,256) ,
                new SqlParameter("@settledate", SqlDbType.DateTime) ,
                new SqlParameter("@isauth", SqlDbType.Int,4) ,
                new SqlParameter("@isfile", SqlDbType.Int,4) ,
                new SqlParameter("@Commission", SqlDbType.Decimal,9) ,
                new SqlParameter("@settlecycle", SqlDbType.Int,4) ,
                new SqlParameter("@Advance", SqlDbType.Decimal,9) ,
                new SqlParameter("@Mergeorderremark", SqlDbType.VarChar,256) ,
                new SqlParameter("@delflag", SqlDbType.Int,4) ,
                new SqlParameter("@deluser", SqlDbType.VarChar,50) ,
                new SqlParameter("@deltime", SqlDbType.DateTime) ,
                new SqlParameter("@shopauthuser", SqlDbType.VarChar,50) ,
                new SqlParameter("@shopfileuser", SqlDbType.VarChar,50),
                new SqlParameter("@delivergetfee_base", SqlDbType.Decimal,9) ,
                new SqlParameter("@DeliverGetSendFee", SqlDbType.Decimal,9) ,
            };

            parameters[0].Value = model.InUse;
            parameters[1].Value = model.OrderDateTime;
            parameters[2].Value = model.OrderChecker;
            parameters[3].Value = model.OrderStatus;
            parameters[4].Value = model.OrderRcver;
            parameters[5].Value = model.OrderComm;
            parameters[6].Value = model.OrderAddress;
            parameters[7].Value = model.AddressText;
            parameters[8].Value = model.OrderAddrEx;
            parameters[9].Value = model.OrderAttach;
            parameters[10].Value = model.OrderSums;
            parameters[11].Value = model.Sender;
            parameters[12].Value = model.SendTime;
            parameters[13].Value = model.CallPhoneNo;
            parameters[14].Value = model.P2Sign;
            parameters[15].Value = model.SendFee;
            parameters[16].Value = model.paymode;
            parameters[17].Value = model.paytime;
            parameters[18].Value = model.paymoney;
            parameters[19].Value = model.paystate;
            parameters[20].Value = model.SetStateTime;
            parameters[21].Value = model.UserId;
            parameters[22].Value = model.TogoId;
            parameters[23].Value = model.orderid;
            parameters[24].Value = model.fromweb;
            parameters[25].Value = model.OldStatus;
            parameters[26].Value = model.SystemUserId;
            parameters[27].Value = model.Commentstate;
            parameters[28].Value = model.writer;
            parameters[29].Value = model.IsShopSet;
            parameters[30].Value = model.cityid;
            parameters[31].Value = model.deliversiteid;
            parameters[32].Value = model.deliverheaderid;
            parameters[33].Value = model.deliverid;
            parameters[34].Value = model.sendstate;
            parameters[35].Value = model.ReveInt1;
            parameters[36].Value = model.ReveInt2;
            parameters[37].Value = model.ReveVar1;
            parameters[38].Value = model.ReveVar2;
            parameters[39].Value = model.ReveDate1;
            parameters[40].Value = model.ReveDate2;
            parameters[41].Value = 0;
            parameters[42].Value = model.OldPrice;
            parameters[43].Value = model.shopdiscountmoney;
            parameters[44].Value = model.tempcode;
            parameters[45].Value = model.cardpay;
            parameters[46].Value = model.cardids;
            parameters[47].Value = model.pointrat;
            parameters[48].Value = model.Packagefee;
            parameters[49].Value = model.settleuser;
            parameters[50].Value = Convert.ToDateTime("1970-1-1");
            parameters[51].Value = model.isauth;
            parameters[52].Value = model.isfile;
            parameters[53].Value = model.Commission;
            parameters[54].Value = model.settlecycle;
            parameters[55].Value = model.Advance;
            parameters[56].Value = model.Mergeorderremark;
            parameters[57].Value = model.delflag;
            parameters[58].Value = model.deluser;
            parameters[59].Value = model.deltime;
            parameters[60].Value = model.shopauthuser;
            parameters[61].Value = model.shopfileuser;

            parameters[62].Value = model.delivergetfee_base;
            parameters[63].Value = model.DeliverGetSendFee;

            SQLHelper.ExecuteNonQuery(CommandType.StoredProcedure, "Custorder_ADD", parameters);

            return 1;
        }

        /// <summary>
        /// 增加饿了么一条数据
        /// </summary>
        public int Addele(Hangjing.Model.CustorderInfo model)
        {
            SqlParameter[] parameters =
            {
                new SqlParameter("@InUse", SqlDbType.VarChar,50) ,
                new SqlParameter("@OrderDateTime", SqlDbType.DateTime) ,
                new SqlParameter("@OrderChecker", SqlDbType.Int,4) ,
                new SqlParameter("@OrderStatus", SqlDbType.Int,4) ,
                new SqlParameter("@OrderRcver", SqlDbType.VarChar,50) ,
                new SqlParameter("@OrderComm", SqlDbType.VarChar,50) ,
                new SqlParameter("@OrderAddress", SqlDbType.VarChar,50) ,
                new SqlParameter("@AddressText", SqlDbType.VarChar,100) ,
                new SqlParameter("@OrderAddrEx", SqlDbType.VarChar,300) ,
                new SqlParameter("@OrderAttach", SqlDbType.VarChar,200) ,
                new SqlParameter("@OrderSums", SqlDbType.Decimal,5) ,
                new SqlParameter("@Sender", SqlDbType.VarChar,256) ,
                new SqlParameter("@SendTime", SqlDbType.DateTime) ,
                new SqlParameter("@CallPhoneNo", SqlDbType.VarChar,256) ,
                new SqlParameter("@P2Sign", SqlDbType.VarChar,256) ,
                new SqlParameter("@SendFee", SqlDbType.Decimal,5) ,
                new SqlParameter("@paymode", SqlDbType.Int,4) ,
                new SqlParameter("@paytime", SqlDbType.DateTime) ,
                new SqlParameter("@paymoney", SqlDbType.Decimal,5) ,
                new SqlParameter("@paystate", SqlDbType.Int,4) ,
                new SqlParameter("@SetStateTime", SqlDbType.DateTime) ,
                new SqlParameter("@UserId", SqlDbType.Int,4) ,
                new SqlParameter("@TogoId", SqlDbType.Int,4) ,
                new SqlParameter("@orderid", SqlDbType.VarChar,50) ,
                new SqlParameter("@fromweb", SqlDbType.VarChar,50) ,
                new SqlParameter("@oldstatus", SqlDbType.Int,4) ,
                new SqlParameter("@systemuserid", SqlDbType.Int,4) ,
                new SqlParameter("@Commentstate", SqlDbType.Int,4) ,
                new SqlParameter("@writer", SqlDbType.VarChar,50) ,
                new SqlParameter("@IsShopSet", SqlDbType.Int,4) ,
                new SqlParameter("@CityID", SqlDbType.Int,4) ,
                new SqlParameter("@deliversiteid", SqlDbType.Int,4) ,
                new SqlParameter("@deliverheaderid", SqlDbType.Int,4) ,
                new SqlParameter("@deliverid", SqlDbType.Int,4) ,
                new SqlParameter("@sendstate", SqlDbType.Int,4) ,
                new SqlParameter("@ReveInt1", SqlDbType.Int,4) ,
                new SqlParameter("@ReveInt2", SqlDbType.Int,4) ,
                new SqlParameter("@ReveVar1", SqlDbType.VarChar,256) ,
                new SqlParameter("@ReveVar2", SqlDbType.VarChar,256) ,
                new SqlParameter("@ReveDate1", SqlDbType.DateTime) ,
                new SqlParameter("@ReveDate2", SqlDbType.DateTime) ,
                new SqlParameter("@isaddpoint", SqlDbType.Int,4) ,
                new SqlParameter("@OldPrice", SqlDbType.Decimal,5) ,
                new SqlParameter("@shopdiscountmoney", SqlDbType.Decimal,5) ,
                new SqlParameter("@tempcode", SqlDbType.VarChar,256) ,
                new SqlParameter("@cardpay", SqlDbType.Decimal,5) ,
                new SqlParameter("@cardids", SqlDbType.VarChar,256) ,
                new SqlParameter("@pointrat", SqlDbType.Int,4) ,
                new SqlParameter("@Packagefee", SqlDbType.Decimal,9) ,
                new SqlParameter("@settleuser", SqlDbType.VarChar,256) ,
                new SqlParameter("@settledate", SqlDbType.DateTime) ,
                new SqlParameter("@isauth", SqlDbType.Int,4) ,
                new SqlParameter("@isfile", SqlDbType.Int,4) ,
                new SqlParameter("@Commission", SqlDbType.Decimal,9) ,
                new SqlParameter("@settlecycle", SqlDbType.Int,4) ,
                new SqlParameter("@Advance", SqlDbType.Decimal,9) ,
                new SqlParameter("@Mergeorderremark", SqlDbType.VarChar,256) ,
                new SqlParameter("@delflag", SqlDbType.Int,4) ,
                new SqlParameter("@deluser", SqlDbType.VarChar,50) ,
                new SqlParameter("@deltime", SqlDbType.DateTime) ,
                new SqlParameter("@shopauthuser", SqlDbType.VarChar,50) ,
                new SqlParameter("@shopfileuser", SqlDbType.VarChar,50),
            };

            parameters[0].Value = model.InUse;
            parameters[1].Value = model.OrderDateTime;
            parameters[2].Value = model.OrderChecker;
            parameters[3].Value = model.OrderStatus;
            parameters[4].Value = model.OrderRcver;
            parameters[5].Value = model.OrderComm;
            parameters[6].Value = model.OrderAddress;
            parameters[7].Value = model.AddressText;
            parameters[8].Value = model.OrderAddrEx;
            parameters[9].Value = model.OrderAttach;
            parameters[10].Value = model.OrderSums;
            parameters[11].Value = model.Sender;
            parameters[12].Value = model.SendTime;
            parameters[13].Value = model.CallPhoneNo;
            parameters[14].Value = model.P2Sign;
            parameters[15].Value = model.SendFee;
            parameters[16].Value = model.paymode;
            parameters[17].Value = model.paytime;
            parameters[18].Value = model.paymoney;
            parameters[19].Value = model.paystate;
            parameters[20].Value = model.SetStateTime;
            parameters[21].Value = model.UserId;
            parameters[22].Value = model.TogoId;
            parameters[23].Value = model.orderid;
            parameters[24].Value = model.fromweb;
            parameters[25].Value = model.OldStatus;
            parameters[26].Value = model.SystemUserId;
            parameters[27].Value = model.Commentstate;
            parameters[28].Value = model.writer;
            parameters[29].Value = model.IsShopSet;
            parameters[30].Value = model.cityid;
            parameters[31].Value = model.deliversiteid;
            parameters[32].Value = model.deliverheaderid;
            parameters[33].Value = model.deliverid;
            parameters[34].Value = model.sendstate;
            parameters[35].Value = model.ReveInt1;
            parameters[36].Value = model.ReveInt2;
            parameters[37].Value = model.ReveVar1;
            parameters[38].Value = model.ReveVar2;
            parameters[39].Value = model.ReveDate1;
            parameters[40].Value = model.ReveDate2;
            parameters[41].Value = 0;
            parameters[42].Value = model.OldPrice;
            parameters[43].Value = model.shopdiscountmoney;
            parameters[44].Value = model.tempcode;
            parameters[45].Value = model.cardpay;
            parameters[46].Value = model.cardids;
            parameters[47].Value = model.pointrat;
            parameters[48].Value = model.Packagefee;
            parameters[49].Value = model.settleuser;
            parameters[50].Value = Convert.ToDateTime("1970-1-1");
            parameters[51].Value = model.isauth;
            parameters[52].Value = model.isfile;
            parameters[53].Value = model.Commission;
            parameters[54].Value = model.settlecycle;
            parameters[55].Value = model.Advance;
            parameters[56].Value = model.Mergeorderremark;
            parameters[57].Value = model.delflag;
            parameters[58].Value = model.deluser;
            parameters[59].Value = model.deltime;
            parameters[60].Value = model.shopauthuser;
            parameters[61].Value = model.shopfileuser;

            SQLHelper.ExecuteNonQuery(CommandType.StoredProcedure, "Custorder_ADDele", parameters);

            return 1;
        }



        /// <summary>
        /// 更新一条数据
        /// </summary>
        public int Update(Hangjing.Model.CustorderInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update custorder set ");
            strSql.Append("InUse=@InUse,");
            strSql.Append("OrderDateTime=@OrderDateTime,");
            strSql.Append("OrderChecker=@OrderChecker,");
            strSql.Append("OrderStatus=@OrderStatus,");
            strSql.Append("OrderRcver=@OrderRcver,");
            strSql.Append("OrderComm=@OrderComm,");
            strSql.Append("OrderAddress=@OrderAddress,");
            strSql.Append("AddressText=@AddressText,");
            strSql.Append("OrderAddrEx=@OrderAddrEx,");
            strSql.Append("OrderAttach=@OrderAttach,");
            strSql.Append("OrderSums=@OrderSums,");
            strSql.Append("Sender=@Sender,");
            strSql.Append("SendTime=@SendTime,");
            strSql.Append("CallPhoneNo=@CallPhoneNo,");
            strSql.Append("P2Sign=@P2Sign,");
            strSql.Append("SendFee=@SendFee,");
            strSql.Append("paymode=@paymode,");
            strSql.Append("paytime=@paytime,");
            strSql.Append("paymoney=@paymoney,");
            strSql.Append("paystate=@paystate,");

            strSql.Append("delivergetfee_base='" + model.delivergetfee_base + "',");

            strSql.Append("shopauthuser=@shopauthuser,");

            strSql.Append("cardids=@cardids,");
            strSql.Append("shopdiscountmoney=@shopdiscountmoney,");

            strSql.Append(" cardpay = @cardpay , ");
            strSql.Append(" Commission = @Commission , ");
            strSql.Append(" Advance = @Advance , ");
            strSql.Append(" Mergeorderremark = @Mergeorderremark,  ");

            strSql.Append(" SystemUserId = @SystemUserId , ");



            strSql.Append("SetStateTime=@SetStateTime,");
            strSql.Append("UserId=@UserId,");
            strSql.Append("TogoId=@TogoId ");
            strSql.Append(" where Unid=@Unid ");
            SqlParameter[] parameters =
            {
                new SqlParameter("@Unid", SqlDbType.Int,4),
                new SqlParameter("@InUse", SqlDbType.VarChar,10),
                new SqlParameter("@OrderDateTime", SqlDbType.DateTime),
                new SqlParameter("@OrderChecker", SqlDbType.Int,4),
                new SqlParameter("@OrderStatus", SqlDbType.Int,4),
                new SqlParameter("@OrderRcver", SqlDbType.VarChar,50),
                new SqlParameter("@OrderComm", SqlDbType.VarChar,50),
                new SqlParameter("@OrderAddress", SqlDbType.VarChar,50),
                new SqlParameter("@AddressText", SqlDbType.VarChar,100),
                new SqlParameter("@OrderAddrEx", SqlDbType.VarChar,300),
                new SqlParameter("@OrderAttach", SqlDbType.VarChar,200),
                new SqlParameter("@OrderSums", SqlDbType.Decimal,9),
                new SqlParameter("@Sender", SqlDbType.VarChar,256),
                new SqlParameter("@SendTime", SqlDbType.DateTime),
                new SqlParameter("@CallPhoneNo", SqlDbType.VarChar,256),
                new SqlParameter("@P2Sign", SqlDbType.VarChar,256),
                new SqlParameter("@SendFee", SqlDbType.Decimal,9),
                new SqlParameter("@paymode", SqlDbType.Int,4),
                new SqlParameter("@paytime", SqlDbType.DateTime),
                new SqlParameter("@paymoney", SqlDbType.Decimal,9),
                new SqlParameter("@paystate", SqlDbType.Int,4),
                new SqlParameter("@SetStateTime", SqlDbType.DateTime),
                new SqlParameter("@UserId", SqlDbType.Int,11),
                new SqlParameter("@TogoId", SqlDbType.Int,11),
                new SqlParameter("@cardids", SqlDbType.VarChar,256),
                new SqlParameter("@shopdiscountmoney", SqlDbType.Decimal,9),
                new SqlParameter("@Commission", SqlDbType.Decimal,9) ,
                new SqlParameter("@Advance", SqlDbType.Decimal,9) ,
                new SqlParameter("@Mergeorderremark", SqlDbType.VarChar,256),
                new SqlParameter("@cardpay", SqlDbType.Decimal,9),
                new SqlParameter("@SystemUserId", SqlDbType.Int,11),
                new SqlParameter("@shopauthuser", SqlDbType.VarChar,256),


            };
            parameters[0].Value = model.Unid;
            parameters[1].Value = model.InUse;
            parameters[2].Value = model.OrderDateTime;
            parameters[3].Value = model.OrderChecker;
            parameters[4].Value = model.OrderStatus;
            parameters[5].Value = model.OrderRcver;
            parameters[6].Value = model.OrderComm;
            parameters[7].Value = model.OrderAddress;
            parameters[8].Value = model.AddressText;
            parameters[9].Value = model.OrderAddrEx;
            parameters[10].Value = model.OrderAttach;
            parameters[11].Value = model.OrderSums;
            parameters[12].Value = model.Sender;
            parameters[13].Value = model.SendTime;
            parameters[14].Value = model.CallPhoneNo;
            parameters[15].Value = model.P2Sign;
            parameters[16].Value = model.SendFee;
            parameters[17].Value = model.paymode;
            parameters[18].Value = model.paytime;
            parameters[19].Value = model.paymoney;
            parameters[20].Value = model.paystate;
            parameters[21].Value = model.SetStateTime;
            parameters[22].Value = model.UserId;
            parameters[23].Value = model.TogoId;
            parameters[24].Value = model.cardids;
            parameters[25].Value = model.shopdiscountmoney;

            parameters[26].Value = model.Commission;
            parameters[27].Value = model.Advance;
            parameters[28].Value = model.Mergeorderremark;
            parameters[29].Value = model.cardpay;
            parameters[30].Value = model.SystemUserId;
            parameters[31].Value = model.shopauthuser;

            return SQLHelper.ExecuteNonQuery(CommandType.Text, strSql.ToString(), parameters);
        }

        public IList<CustorderInfo> GetInderModel()
        {
            IList<CustorderInfo> infos = new List<CustorderInfo>();


            string sql = "select top(10) Unid,OrderDateTime,OrderRcver,OrderSums,TogoId,(select Name from Points where Unid=custorder.TogoId) as TogoName,(select top(1)  FoodUnid from Foodlist where orderid=custorder.orderid) as Foodid   from custorder   where  OrderStatus=@OrderStatus and TogoId<>1 order by Unid desc ";

            SqlParameter parameter = new SqlParameter("@OrderStatus", SqlDbType.Int, 4);
            parameter.Value = 3;
            using (SqlDataReader dr = SQLHelper.ExecuteReader(CommandType.Text, sql, parameter))
            {
                while (dr.Read())
                {
                    CustorderInfo info = new CustorderInfo();
                    info.Unid = HJConvert.ToInt32(dr["Unid"]);
                    info.OrderDateTime = HJConvert.ToDateTime(dr["OrderDateTime"]);
                    info.OrderRcver = HJConvert.ToString(dr["OrderRcver"]);
                    info.TogoName = HJConvert.ToString(dr["TogoName"]);
                    info.OrderSums = HJConvert.ToDecimal(dr["OrderSums"]);
                    info.TogoId = HJConvert.ToInt32(dr["TogoId"]);
                    info.FoodName = (string)SQLHelper.ExecuteScalar(CommandType.Text, "select FoodName from Foodinfo where Unid=" + HJConvert.ToInt32(dr["Foodid"]) + "  ", null);//
                    infos.Add(info);
                }
            }
            return infos;
        }
        /// <summary>
        /// 获取记录所有字段
        /// </summary>
        /// 此代码由杭景科技代码内部生成器自动生成
        /// <param>Unid</param>
        /// <returns>CustorderInfo</returns>
        public CustorderInfo GetModel(int Unid)
        {

            return GetModel(Unid.ToString());
        }


        /// <summary>
        /// 获取记录所有字段
        /// </summary>
        /// 此代码由杭景科技代码内部生成器自动生成
        /// <param>Unid</param>
        /// <returns>CustorderInfo</returns>
        public CustorderInfo GetModel(string orderid)
        {
            string sql = "select a.*,b.name as TogoName,c.name as CustomerName from custorder as a left join points as b on a.togoid = b.unid left join ecustomer as c on a.userid =c.dataid where ";
            if (orderid.Length >= 9)
            {
                sql += " a.orderid= '" + orderid.Trim() + "'";
            }
            else
            {
                sql += " a.Unid=" + orderid;
            }

            CustorderInfo model = null;
            using (SqlDataReader dr = SQLHelper.ExecuteReader(CommandType.Text, sql, null))
            {
                if (dr.Read())
                {
                    model = new CustorderInfo();
                    model.Unid = HJConvert.ToInt32(dr["Unid"]);
                    model.InUse = HJConvert.ToString(dr["InUse"]);
                    model.OrderDateTime = HJConvert.ToDateTime(dr["OrderDateTime"]);
                    model.OrderChecker = HJConvert.ToInt32(dr["OrderChecker"]);
                    model.OrderStatus = HJConvert.ToInt32(dr["OrderStatus"]);
                    model.OrderRcver = HJConvert.ToString(dr["OrderRcver"]);
                    model.OrderComm = HJConvert.ToString(dr["OrderComm"]);
                    model.OrderAddress = HJConvert.ToString(dr["OrderAddress"]);
                    model.AddressText = HJConvert.ToString(dr["AddressText"]);
                    model.OrderAddrEx = HJConvert.ToString(dr["OrderAddrEx"]);
                    model.OrderAttach = HJConvert.ToString(dr["OrderAttach"]);
                    model.OrderSums = HJConvert.ToDecimal(dr["OrderSums"]);
                    model.Sender = HJConvert.ToString(dr["Sender"]);
                    model.SendTime = HJConvert.ToDateTime(dr["SendTime"]);
                    model.CallPhoneNo = HJConvert.ToString(dr["CallPhoneNo"]);
                    model.P2Sign = HJConvert.ToString(dr["P2Sign"]);
                    model.SendFee = HJConvert.ToDecimal(dr["SendFee"]);
                    model.paymode = HJConvert.ToInt32(dr["paymode"]);
                    model.paytime = HJConvert.ToDateTime(dr["paytime"]);
                    model.paymoney = HJConvert.ToDecimal(dr["paymoney"]);
                    model.paystate = HJConvert.ToInt32(dr["paystate"]);
                    model.SetStateTime = HJConvert.ToDateTime(dr["SetStateTime"]);
                    model.UserId = HJConvert.ToInt32(dr["UserId"]);
                    model.TogoId = HJConvert.ToInt32(dr["TogoId"]);
                    model.TogoName = HJConvert.ToString(dr["TogoName"]);
                    model.orderid = HJConvert.ToString(dr["orderid"]);
                    model.SystemUserId = HJConvert.ToInt32(dr["SystemUserId"]);
                    model.OldStatus = HJConvert.ToInt32(dr["OldStatus"]);
                    model.IsShopSet = HJConvert.ToInt32(dr["IsShopSet"]);
                    model.deliversiteid = HJConvert.ToInt32(dr["deliversiteid"]);
                    model.deliverheaderid = HJConvert.ToInt32(dr["deliverheaderid"]);
                    model.deliverid = HJConvert.ToInt32(dr["deliverid"]);
                    model.sendstate = HJConvert.ToInt32(dr["sendstate"]);
                    model.ReveInt1 = HJConvert.ToInt32(dr["ReveInt1"]);

                    model.cityid = HJConvert.ToInt32(dr["cityid"]);

                    model.ReveInt2 = HJConvert.ToInt32(dr["ReveInt2"]);
                    model.ReveVar1 = HJConvert.ToString(dr["ReveVar1"]);
                    model.ReveVar2 = HJConvert.ToString(dr["ReveVar2"]);
                    model.ReveDate1 = HJConvert.ToDateTime(dr["ReveDate1"]);
                    model.ReveDate2 = HJConvert.ToDateTime(dr["ReveDate2"]);
                    model.OldPrice = HJConvert.ToDecimal(dr["OldPrice"]);
                    model.fromweb = HJConvert.ToString(dr["fromweb"]);
                    model.shopdiscountmoney = HJConvert.ToDecimal(dr["shopdiscountmoney"]);
                    model.cardpay = HJConvert.ToDecimal(dr["cardpay"]);
                    model.Packagefee = HJConvert.ToDecimal(dr["Packagefee"]);
                    model.cardids = HJConvert.ToString(dr["cardids"]);
                    model.CustomerName = HJConvert.ToString(dr["CustomerName"]);

                    model.Commission = HJConvert.ToDecimal(dr["Commission"]);
                    model.settlecycle = HJConvert.ToInt32(dr["settlecycle"]);
                    model.Advance = HJConvert.ToDecimal(dr["Advance"]);
                    model.Mergeorderremark = HJConvert.ToString(dr["Mergeorderremark"]);
                    model.shopauthuser = HJConvert.ToString(dr["shopauthuser"]);

                    model.delivergetfee_base = HJConvert.ToDecimal(dr["delivergetfee_base"]);
                    model.DeliverGetSendFee = HJConvert.ToDecimal(dr["DeliverGetSendFee"]);

                    model.isaddpoint = HJConvert.ToInt32(dr["isaddpoint"]);
                   
                    model.needpaymoney = model.OrderSums;
                }
            }
            return model;
        }

        /// <summary>
        /// 是否可评论
        /// </summary>
        /// <param name="UserID"></param>
        /// <returns></returns>
        public int OrderDataID(int UserID, int id)
        {
            return Convert.ToInt32(SQLHelper.ExecuteScalar(CommandType.Text, "select  Unid from Custorder where Commentstate=0 and UserId=" + UserID + "and TogoId=" + id + "and OrderDateTime between '" + DateTime.Now.ToShortDateString() + "' and  '" + DateTime.Now.ToString() + "'", null));
        }
        public int OrderDataIDstate(int UserID, int id)
        {
            return Convert.ToInt32(SQLHelper.ExecuteScalar(CommandType.Text, "select  Unid from Custorder where Commentstate=1 and UserId=" + UserID + " and TogoId=" + id + " ", null));
        }
        //评论更新订单已评论了
        public void OrderUnid(int Unid)
        {
            int i = Convert.ToInt32(SQLHelper.ExecuteScalar(CommandType.Text, "update Custorder set Commentstate=1 where Unid=" + Unid + "", null));
        }

        /// <summary>
        /// 获得总记录数
        /// </summary>
        /// 此代码由杭景科技代码内部生成器自动生成
        public int GetCount(string strWhere)
        {
            return Convert.ToInt32(SQLHelper.ExecuteScalar(CommandType.StoredProcedure, "pagecount", new SqlParameter("@tblName", "custorder"), new SqlParameter("@strWhere", strWhere)));
        }

        public int GetCountFix(string strWhere)
        {
            string sql = "SELECT COUNT(*) FROM custorder left JOIN points ON custorder.TogoId = points.unid left JOIN Deliver ON Deliver.DataId = custorder.deliverid ";

            if (string.IsNullOrEmpty(strWhere) == false)
            {
                sql += " WHERE " + strWhere;
            }

            return Convert.ToInt32(SQLHelper.ExecuteScalar(CommandType.Text, sql, null));
        }

        /// <summary>
        /// 后台订单管理列表
        /// </summary>
        /// <param name="strWhere"></param>
        /// <returns></returns>
        public int GetAdminOrdersCount(string strWhere)
        {
            string sql = "SELECT COUNT(*) FROM custorder left JOIN points  with(nolock)  ON custorder.TogoId = points.unid left JOIN Deliver  with(nolock)  ON Deliver.DataId = custorder.deliverid left JOIN OrderDeliver ON custorder.orderid = OrderDeliver.orderid ";

            if (string.IsNullOrEmpty(strWhere) == false)
            {
                sql += " WHERE " + strWhere;
            }

            return Convert.ToInt32(SQLHelper.ExecuteScalar(CommandType.Text, sql, null));
        }

        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="pagesize">页尺寸</param>
        /// <param name="pageindex">页索引</param>
        /// <param name="strWhere">查询条件</param>
        /// <param name="orderName">排序字段</param>
        /// <param name="orderType">排序类型，1为降序，0为升序</param>
        /// <returns>图书列表</returns>
        ///此代码由杭景科技代码内部生成器自动生成
        public IList<CustorderInfo> GetList(int pagesize, int pageindex, string strWhere, string orderName, int orderType)
        {
            IList<CustorderInfo> infos = new List<CustorderInfo>();
            SqlParameter[] parameters =
            {
                new SqlParameter("@tblName", SqlDbType.VarChar,255),
                new SqlParameter("@strGetFields", SqlDbType.VarChar,1000),
                new SqlParameter("@primary", SqlDbType.VarChar,255),
                new SqlParameter("@orderName", SqlDbType.VarChar,255),
                new SqlParameter("@PageSize", SqlDbType.Int),
                new SqlParameter("@PageIndex", SqlDbType.Int),
                new SqlParameter("@OrderType", SqlDbType.Bit),
                new SqlParameter("@strWhere", SqlDbType.VarChar,1500)
            };
            parameters[0].Value = "custorder";
            parameters[1].Value = "*,(select Name from Points where Unid=custorder.TogoId) as TogoName";
            parameters[2].Value = "Unid";
            parameters[3].Value = orderName;
            parameters[4].Value = pagesize;
            parameters[5].Value = pageindex;
            parameters[6].Value = orderType;
            parameters[7].Value = strWhere;
            using (SqlDataReader dr = SQLHelper.ExecuteReader(CommandType.StoredProcedure, "pageselectpri", parameters))
            {
                while (dr.Read())
                {
                    CustorderInfo info = new CustorderInfo();
                    info.Unid = HJConvert.ToInt32(dr["Unid"]);
                    info.InUse = HJConvert.ToString(dr["InUse"]);
                    info.OrderDateTime = HJConvert.ToDateTime(dr["OrderDateTime"]);
                    info.OrderChecker = HJConvert.ToInt32(dr["OrderChecker"]);
                    info.OrderStatus = HJConvert.ToInt32(dr["OrderStatus"]);
                    info.OrderRcver = HJConvert.ToString(dr["OrderRcver"]);
                    info.OrderComm = HJConvert.ToString(dr["OrderComm"]);
                    info.OrderAddress = HJConvert.ToString(dr["OrderAddress"]);
                    info.AddressText = HJConvert.ToString(dr["AddressText"]);
                    info.OrderAddrEx = HJConvert.ToString(dr["OrderAddrEx"]);
                    info.OrderAttach = HJConvert.ToString(dr["OrderAttach"]);
                    info.OrderSums = HJConvert.ToDecimal(dr["OrderSums"]);
                    info.Sender = HJConvert.ToString(dr["Sender"]);
                    info.SendTime = HJConvert.ToDateTime(dr["SendTime"]);
                    info.CallPhoneNo = HJConvert.ToString(dr["CallPhoneNo"]);
                    info.P2Sign = HJConvert.ToString(dr["P2Sign"]);
                    info.SendFee = HJConvert.ToDecimal(dr["SendFee"]);
                    info.paymode = HJConvert.ToInt32(dr["paymode"]);
                    info.paytime = HJConvert.ToDateTime(dr["paytime"]);
                    info.paymoney = HJConvert.ToDecimal(dr["paymoney"]);
                    info.paystate = HJConvert.ToInt32(dr["paystate"]);
                    info.SetStateTime = HJConvert.ToDateTime(dr["SetStateTime"]);
                    info.UserId = HJConvert.ToInt32(dr["UserId"]);
                    info.TogoId = HJConvert.ToInt32(dr["TogoId"]);
                    info.deliverid = HJConvert.ToInt32(dr["deliverid"]);
                    info.TogoName = HJConvert.ToString(dr["TogoName"]);
                    info.orderid = HJConvert.ToString(dr["orderid"]);
                    info.ReveInt1 = HJConvert.ToInt32(dr["ReveInt1"]);
                    info.ReveInt2 = HJConvert.ToInt32(dr["ReveInt2"]);
                    info.IsShopSet = HJConvert.ToInt32(dr["IsShopSet"]);
                    info.sendstate = HJConvert.ToInt32(dr["sendstate"]);
                    info.SystemUserId = HJConvert.ToInt32(dr["SystemUserId"]);
                    info.cardpay = HJConvert.ToDecimal(dr["cardpay"]);
                    info.Packagefee = HJConvert.ToDecimal(dr["Packagefee"]);
                    info.shopdiscountmoney = HJConvert.ToDecimal(dr["shopdiscountmoney"]);
                    info.DeliverGetSendFee = HJConvert.ToDecimal(dr["DeliverGetSendFee"]);
                    info.pointrat = HJConvert.ToInt32(dr["pointrat"]);
                    info.Commentstate = HJConvert.ToInt32(dr["Commentstate"]);
                    info.Advance = HJConvert.ToDecimal(dr["advance"]);
                    info.Commission = HJConvert.ToDecimal(dr["Commission"]);
                    info.ReveVar2 = HJConvert.ToString(dr["ReveVar2"]);


                    info.Mergeorderremark = HJConvert.ToString(dr["Mergeorderremark"]);

                    infos.Add(info);
                }
            }
            return infos;
        }
        /// <summary>
        /// 查询打印
        /// <returns>图书列表</returns>
        ///此代码由杭景科技代码内部生成器自动生成
        public IList<CustorderInfo> GetListt(string strWhere)
        {
            IList<CustorderInfo> infos = new List<CustorderInfo>();

            using (SqlDataReader dr = SQLHelper.ExecuteReader(CommandType.Text, "select *,(select Name from Points where Unid=custorder.TogoId) as TogoName,(select Name from  ECustomer where DataId=custorder.UserId) as CustomerName  from custorder where Unid in(" + strWhere + ")", null))
            {
                while (dr.Read())
                {
                    CustorderInfo info = new CustorderInfo();
                    info.Unid = HJConvert.ToInt32(dr["Unid"]);
                    info.InUse = HJConvert.ToString(dr["InUse"]);
                    info.OrderDateTime = HJConvert.ToDateTime(dr["OrderDateTime"]);
                    info.OrderChecker = HJConvert.ToInt32(dr["OrderChecker"]);
                    info.OrderStatus = HJConvert.ToInt32(dr["OrderStatus"]);
                    info.OrderRcver = HJConvert.ToString(dr["OrderRcver"]);
                    info.OrderComm = HJConvert.ToString(dr["OrderComm"]);
                    info.OrderAddress = HJConvert.ToString(dr["OrderAddress"]);
                    info.AddressText = HJConvert.ToString(dr["AddressText"]);
                    info.OrderAddrEx = HJConvert.ToString(dr["OrderAddrEx"]);
                    info.OrderAttach = HJConvert.ToString(dr["OrderAttach"]);
                    info.OrderSums = HJConvert.ToDecimal(dr["OrderSums"]);
                    info.Sender = HJConvert.ToString(dr["Sender"]);
                    info.SendTime = HJConvert.ToDateTime(dr["SendTime"]);
                    info.CallPhoneNo = HJConvert.ToString(dr["CallPhoneNo"]);
                    info.P2Sign = HJConvert.ToString(dr["P2Sign"]);
                    info.SendFee = HJConvert.ToDecimal(dr["SendFee"]);
                    info.paymode = HJConvert.ToInt32(dr["paymode"]);
                    info.paytime = HJConvert.ToDateTime(dr["paytime"]);
                    info.paymoney = HJConvert.ToDecimal(dr["paymoney"]);
                    info.paystate = HJConvert.ToInt32(dr["paystate"]);
                    info.SetStateTime = HJConvert.ToDateTime(dr["SetStateTime"]);
                    info.UserId = HJConvert.ToInt32(dr["UserId"]);
                    info.TogoId = HJConvert.ToInt32(dr["TogoId"]);
                    info.TogoName = HJConvert.ToString(dr["TogoName"]);
                    info.CustomerName = HJConvert.ToString(dr["CustomerName"]);
                    // info.TogoTel = HJConvert.ToString(dr["TogoTel"]);
                    info.orderid = HJConvert.ToString(dr["orderid"]);
                    infos.Add(info);
                }
            }
            return infos;
        }



        /// <summary>
        /// 获取订单的配送员编号
        /// <returns>图书列表</returns>
        ///此代码由杭景科技代码内部生成器自动生成
        public IList<CustorderInfo> GetOrderDeliverIDs(string strWhere)
        {
            IList<CustorderInfo> infos = new List<CustorderInfo>();

            using (SqlDataReader dr = SQLHelper.ExecuteReader(CommandType.Text, "select deliverid  from custorder where Unid in(" + strWhere + ") ", null))
            {
                while (dr.Read())
                {
                    CustorderInfo info = new CustorderInfo();
                    info.deliverid = HJConvert.ToInt32(dr["deliverid"]);
                    infos.Add(info);
                }
            }
            return infos;
        }



        /// <summary>
        /// 获取列表 此方法在翻页最后一页时似乎读取数据失败了 使用的时候注意测试
        /// </summary>
        /// <param name="pagesize">页尺寸</param>
        /// <param name="pageindex">页索引</param>
        /// <param name="strWhere">查询条件</param>
        /// <param name="orderName">排序字段</param>
        /// <param name="orderType">排序类型，1为降序，0为升序</param>
        /// <returns>图书列表</returns>
        ///此代码由杭景科技代码内部生成器自动生成
        public IList<CustorderInfo> GetListFix(int pagesize, int pageindex, string strWhere, string orderName, int orderType)
        {
            IList<CustorderInfo> infos = new List<CustorderInfo>();
            SqlParameter[] parameters =
            {
                new SqlParameter("@tblName", SqlDbType.VarChar,255),
                new SqlParameter("@strGetFields", SqlDbType.VarChar,1000),
                new SqlParameter("@primary", SqlDbType.VarChar,255),
                new SqlParameter("@orderName", SqlDbType.VarChar,255),
                new SqlParameter("@PageSize", SqlDbType.Int),
                new SqlParameter("@PageIndex", SqlDbType.Int),
                new SqlParameter("@OrderType", SqlDbType.Bit),
                new SqlParameter("@strWhere", SqlDbType.VarChar,2500)
            };
            parameters[0].Value = "custorder left JOIN points  with(nolock)  ON custorder.TogoId = points.unid left JOIN Deliver  with(nolock)  ON Deliver.DataId = custorder.deliverid  left JOIN OrderDeliver ON custorder.orderid = OrderDeliver.orderid ";
            parameters[1].Value = "custorder.*,points.name as TogoName,Deliver.Name AS delivername,Deliver.phone as deliverphone,Deliver.GpsIMEI as groupid,points.comm As TogoTel";
            parameters[2].Value = "custorder.Unid";
            parameters[3].Value = orderName;
            parameters[4].Value = pagesize;
            parameters[5].Value = pageindex;
            parameters[6].Value = orderType;
            parameters[7].Value = strWhere;
            using (SqlDataReader dr = SQLHelper.ExecuteReader(CommandType.StoredProcedure, "wg_pageselectprifix", parameters))
            {
                while (dr.Read())
                {
                    CustorderInfo info = new CustorderInfo();
                    info.Unid = HJConvert.ToInt32(dr["Unid"]);
                    info.InUse = HJConvert.ToString(dr["InUse"]);
                    info.OrderDateTime = HJConvert.ToDateTime(dr["OrderDateTime"]);
                    info.OrderChecker = HJConvert.ToInt32(dr["OrderChecker"]);
                    info.OrderStatus = HJConvert.ToInt32(dr["OrderStatus"]);
                    info.OrderRcver = HJConvert.ToString(dr["OrderRcver"]);
                    info.OrderComm = HJConvert.ToString(dr["OrderComm"]);
                    info.OrderAddress = HJConvert.ToString(dr["OrderAddress"]);
                    info.AddressText = HJConvert.ToString(dr["AddressText"]);
                    info.OrderAddrEx = HJConvert.ToString(dr["OrderAddrEx"]);
                    info.OrderAttach = HJConvert.ToString(dr["OrderAttach"]);
                    info.OrderSums = HJConvert.ToDecimal(dr["OrderSums"]);
                    info.Sender = HJConvert.ToString(dr["Sender"]);
                    info.SendTime = HJConvert.ToDateTime(dr["SendTime"]);
                    info.CallPhoneNo = HJConvert.ToString(dr["CallPhoneNo"]);
                    info.P2Sign = HJConvert.ToString(dr["P2Sign"]);
                    info.SendFee = HJConvert.ToDecimal(dr["SendFee"]);
                    info.paymode = HJConvert.ToInt32(dr["paymode"]);
                    info.paytime = HJConvert.ToDateTime(dr["paytime"]);
                    info.paymoney = HJConvert.ToDecimal(dr["paymoney"]);
                    info.cardpay = HJConvert.ToDecimal(dr["cardpay"]);

                    info.paystate = HJConvert.ToInt32(dr["paystate"]);
                    info.SetStateTime = HJConvert.ToDateTime(dr["SetStateTime"]);
                    info.UserId = HJConvert.ToInt32(dr["UserId"]);
                    info.TogoId = HJConvert.ToInt32(dr["TogoId"]);
                    info.deliverid = HJConvert.ToInt32(dr["deliverid"]);
                    info.TogoName = HJConvert.ToString(dr["TogoName"]);
                    info.cityid = HJConvert.ToInt32(dr["cityid"]);
                    info.CustomerName = HJConvert.ToString(dr["delivername"]);
                    info.writer = HJConvert.ToString(dr["writer"]);
                    info.TogoTel = HJConvert.ToString(dr["TogoTel"]);
                    info.orderid = HJConvert.ToString(dr["orderid"]);
                    info.writer = HJConvert.ToString(dr["writer"]);
                    info.ReveVar1 = HJConvert.ToString(dr["ReveVar1"]);
                    info.cardids = HJConvert.ToString(dr["cardids"]);
                    info.shopdiscountmoney = HJConvert.ToDecimal(dr["shopdiscountmoney"]);




                    info.DeliverGetSendFee = HJConvert.ToDecimal(dr["DeliverGetSendFee"]);
                    info.delivergetfee_base = HJConvert.ToDecimal(dr["delivergetfee_base"]);

                    info.OldPrice = HJConvert.ToDecimal(dr["OldPrice"]);
                    info.sendstate = HJConvert.ToInt32(dr["sendstate"]);
                    info.IsShopSet = HJConvert.ToInt32(dr["IsShopSet"]);
                    info.deliversiteid = HJConvert.ToInt32(dr["deliversiteid"]);
                    info.ReveInt1 = HJConvert.ToInt32(dr["ReveInt1"]);
                    info.isauth = HJConvert.ToInt32(dr["isauth"]);
                    info.isfile = HJConvert.ToInt32(dr["isfile"]);

                    info.Commission = HJConvert.ToDecimal(dr["Commission"]);
                    info.settlecycle = HJConvert.ToInt32(dr["settlecycle"]);
                    info.Advance = HJConvert.ToDecimal(dr["Advance"]);
                    info.Mergeorderremark = HJConvert.ToString(dr["Mergeorderremark"]);
                    info.groupid = HJConvert.ToInt32(dr["groupid"]);
                    info.SystemUserId = HJConvert.ToInt32(dr["SystemUserId"]);
                    info.settleuser = HJConvert.ToString(dr["settleuser"]);

                    info.deluser = HJConvert.ToString(dr["deluser"]);
                    info.delflag = HJConvert.ToInt32(dr["delflag"]);
                    info.deltime = HJConvert.ToDateTime(dr["deltime"]);

                    info.shopauthuser = HJConvert.ToString(dr["shopauthuser"]);
                    info.shopfileuser = HJConvert.ToString(dr["shopfileuser"]);
                    info.pointrat = HJConvert.ToInt32(dr["pointrat"]);
                    info.Commentstate = HJConvert.ToInt32(dr["Commentstate"]);
                    info.deliverheaderid = HJConvert.ToInt32(dr["deliverheaderid"]);

                    info.deliverphone = HJConvert.ToString(dr["deliverphone"]);
                    info.fromweb = HJConvert.ToString(dr["fromweb"]);

                    switch (info.OrderStatus)
                    {
                        case 1:
                        case 2:
                        case 7:
                            info.ReveInt1 = Convert.ToInt32((DateTime.Now - info.SendTime).TotalMinutes);
                            break;
                        case 3:
                            break;
                        default:
                            info.ReveInt1 = 0;
                            break;
                    }



                    infos.Add(info);
                }
            }
            return infos;
        }

        /// <summary>
        /// 删除或者恢复
        /// </summary>
        /// <param name="IdList"></param>
        /// <param name="adminname"></param>
        /// <param name="delflag">0表示恢复，1表示删除</param>
        /// <returns></returns>
        public int DelList(string IdList, string adminname)
        {
            return DelOrRecoverOrder(IdList, adminname, 1);
        }

        /// <summary>
        /// 删除或者恢复
        /// </summary>
        /// <param name="IdList"></param>
        /// <param name="adminname"></param>
        /// <param name="delflag">0表示恢复，1表示删除</param>
        /// <returns></returns>
        public int DelOrRecoverOrder(string IdList, string adminname, int delflag)
        {
            StringBuilder str = new StringBuilder();
            //删除其他
            str.Append("UPDATE dbo.Custorder SET delflag = " + delflag + ",deluser = '" + adminname + "',deltime=getdate()  where Unid in ({0});");

            return SQLHelper.ExecuteNonQuery(CommandType.Text, string.Format(str.ToString(), IdList), null);
        }

        /// <summary>
        /// 物理删除
        /// </summary>
        /// <param name="IdList"></param>
        /// <returns></returns>
        /// 此代码由杭景科技代码内部生成器自动生成
        public int CompletelyDelList(string IdList)
        {
            StringBuilder str = new StringBuilder();

            //删除其他
            str.Append("delete from Custorder where Unid in ({0});");

            return SQLHelper.ExecuteNonQuery(CommandType.Text, string.Format(str.ToString(), IdList), null);
        }



        /// <summary>
        /// 根据订单编号更改打印机状态为成功（add by yangxioalong@ihangjing.com）
        /// </summary>
        /// <param name="orderId"></param>
        /// <returns></returns>
        public int UpdateOrderState(int orderId)
        {
            StringBuilder str = new StringBuilder();
            str.Append("update custorder set orderstatus=3 where Unid=@Unid;");

            SqlParameter[] Para =
            {
                new SqlParameter("@Unid",SqlDbType.Int,20)
            };
            Para[0].Value = orderId;
            //AddPoint(orderId);

            return SQLHelper.ExecuteNonQuery(CommandType.Text, str.ToString(), Para);
        }

        /// <summary>
        /// 根据订单编号更改打印机状态为成功（add by yangxioalong@ihangjing.com）
        /// </summary>
        /// <param name="orderId"></param>
        /// <returns></returns>
        public int UpdateOrderState(string orderId)
        {
            StringBuilder str = new StringBuilder();
            str.Append("update custorder set orderstatus=3 where orderId=@orderId;");

            SqlParameter[] Para =
            {
                new SqlParameter("@orderId",SqlDbType.VarChar,50)
            };
            Para[0].Value = orderId;
            AddPoint(orderId);

            return SQLHelper.ExecuteNonQuery(CommandType.Text, str.ToString(), Para);
        }


        /// <summary>
        /// 当打印机获取完订单时，把这个值设置成当前时间。 （add by yangxioalong@ihangjing.com）
        /// 过一个时间段后，如果此订单的状态还没有更改成成功，则为失败。
        /// </summary>
        /// <param name="orderId"></param>
        /// <returns></returns>
        public int UpdateOrderStateTime(int orderId)
        {
            StringBuilder str = new StringBuilder();
            str.Append("update custorder set SetStateTime=" + DateTime.Now + " where Unid=@Unid");

            SqlParameter[] Para =
            {
                new SqlParameter("@Unid",SqlDbType.Int,20)
            };
            Para[0].Value = orderId;

            return SQLHelper.ExecuteNonQuery(CommandType.Text, str.ToString(), Para);
        }

        /// <summary>
        /// 修改订单的状态。
        /// </summary>
        /// <param name="OrderID">订单编号</param>
        /// <param name="State">要修改的订单状态</param>
        public int UpdataState(string OrderID, int State)
        {

            StringBuilder str = new StringBuilder();

            if (State == 2)
            {
                str.Append("update custorder set orderstatus=@orderstatus,SetStateTime='" + DateTime.Now + "' where OrderID=@OrderID");
            }
            else
            {
                str.Append("update custorder set orderstatus=@orderstatus where OrderID=@OrderID");
            }

            SqlParameter[] Para =
            {
                new SqlParameter("@OrderID",SqlDbType.VarChar,50),
                new SqlParameter("@orderstatus",SqlDbType.Int)
            };
            Para[0].Value = OrderID;
            Para[1].Value = State;

            return SQLHelper.ExecuteNonQuery(CommandType.Text, str.ToString(), Para);
        }

        /// <summary>
        /// 修改订单的状态。
        /// </summary>
        /// <param name="OrderID">订单编号</param>
        /// <param name="State">要修改的订单状态</param>
        public int UpdataState(int OrderID, int State)
        {

            StringBuilder str = new StringBuilder();

            if (State == 2)
            {
                str.Append("update custorder set orderstatus=@orderstatus,SetStateTime='" + DateTime.Now + "' where Unid=@Unid");
            }
            else
            {
                str.Append("update custorder set orderstatus=@orderstatus where Unid=@Unid");
            }

            SqlParameter[] Para =
            {
                new SqlParameter("@Unid",SqlDbType.Int,20),
                new SqlParameter("@orderstatus",SqlDbType.Int)
            };
            Para[0].Value = OrderID;
            Para[1].Value = State;

            return SQLHelper.ExecuteNonQuery(CommandType.Text, str.ToString(), Para);
        }

        /// <summary>
        /// 修改订单的处理人
        /// </summary>
        /// <param name="OrderID">订单编号</param>
        /// <param name="State">要修改的订单状态</param>
        public int UpdataState(int OrderID, string State)
        {

            StringBuilder str = new StringBuilder();
            str.Append("update custorder set writer=@writer where Unid=@OrderID");
            SqlParameter[] Para =
            {
                new SqlParameter("@OrderID",SqlDbType.VarChar,20),
                new SqlParameter("@writer",SqlDbType.VarChar , 20)
            };
            Para[0].Value = OrderID;
            Para[1].Value = State;

            return SQLHelper.ExecuteNonQuery(CommandType.Text, str.ToString(), Para);
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public int UpdatePayState(Hangjing.Model.CustorderInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update custorder set ");
            strSql.Append("paymode=@paymode,");
            strSql.Append("paytime=@paytime,");
            strSql.Append("paymoney=@paymoney,");
            strSql.Append("paystate=@paystate,delflag=0 ");
            strSql.Append(" where Unid=@Unid ");
            SqlParameter[] parameters =
            {
                new SqlParameter("@Unid", SqlDbType.Int,20),
                new SqlParameter("@paymode", SqlDbType.Int,4),
                new SqlParameter("@paytime", SqlDbType.DateTime),
                new SqlParameter("@paymoney", SqlDbType.Decimal,5),
                new SqlParameter("@paystate", SqlDbType.Int,4)
            };
            parameters[0].Value = model.Unid;
            parameters[1].Value = model.paymode;
            parameters[2].Value = model.paytime;
            parameters[3].Value = model.paymoney;
            parameters[4].Value = model.paystate;

            return SQLHelper.ExecuteNonQuery(CommandType.Text, strSql.ToString(), parameters);
        }

        /*--月统计
        select month(ordertime),count(orderid) from etogoorder where year(ordertime) = '2010' group by month(ordertime)

        --日统计
        select day(ordertime),count(orderid) from etogoorder where year(ordertime) = '2010' and month(ordertime)='7'  group by day(ordertime)

        --小时统计
        select datepart(hh,ordertime),count(orderid) from etogoorder where year(ordertime) = '2010' and month(ordertime)='7' and day(ordertime)='16'  group by datepart(hh,ordertime)
        */

        /// <summary>
        /// Type:1 小时统计 2 日统计 3 周统计 4 月统计 5 年统计
        /// Year 需要统计的年份 Month需要统计的月份 Day需要统计的日 
        /// 小时统计需要Year Month Day
        /// 日统计需要Year Month 
        /// 月统计需要 Year
        /// </summary>
        /// <param name="Type"></param>
        /// <returns></returns>
        public IList<OrderCountInfo> GetOrderCount(int Type, string Year, string Month, string Day)
        {
            IList<OrderCountInfo> list = new List<OrderCountInfo>();

            string strSql = "";

            switch (Type)
            {
                case 1: strSql = "select datepart(hh,OrderDateTime) as CountKey,count(orderid) as CountIntValue,Sum(OrderSums) as CountDecimalPrice from custorder where year(OrderDateTime) = '" + Year + "' and month(OrderDateTime)='" + Month + "'  and day(OrderDateTime)='" + Day + "'  group by datepart(hh,OrderDateTime)"; break;
                case 2: strSql = "select day(OrderDateTime) as CountKey,count(orderid) as CountIntValue,Sum(OrderSums) as CountDecimalPrice from custorder where year(OrderDateTime) = '" + Year + "' and month(OrderDateTime)='" + Month + "'  group by day(OrderDateTime)"; break;
                case 3: strSql = ""; break;
                case 4: strSql = "select month(OrderDateTime) as CountKey,count(orderid) as CountIntValue,Sum(OrderSums) as CountDecimalPrice from custorder where year(OrderDateTime) = '" + Year + "' group by month(OrderDateTime)"; break;
                case 5: strSql = ""; break;
            }

            OrderCountInfo info = new OrderCountInfo();

            using (SqlDataReader dr = SQLHelper.ExecuteReader(CommandType.Text, strSql, null))
            {
                while (dr.Read())
                {
                    info = new OrderCountInfo();

                    info.CountKey = HJConvert.ToString(dr["CountKey"]);
                    info.CountIntValue = HJConvert.ToInt32(dr["CountIntValue"]);
                    info.CountDecimalValue = HJConvert.ToDecimal(dr["CountDecimalPrice"]);

                    list.Add(info);
                }
            }

            return list;
        }

        public IList<OrderCountInfo> SumOrderByTimeUnit(string where, string timeunit)
        {
            IList<OrderCountInfo> list = new List<OrderCountInfo>();

            string strSql = "";

            switch (timeunit)
            {

                case "day": strSql = "select CONVERT(varchar(100), OrderDateTime, 23) as CountKey,count(1) as CountIntValue,sum (case when deliversiteid =10  then (1) else 0   end)  as deliversiteid,Sum(OrderSums) as CountDecimalPrice,Sum(shopdiscountmoney) as CountSendFee,Sum(ReveInt2) as ReveInt2,SUM(DATEDIFF(Second,DeliveryTime,OverTime) )AS sendtime,SUM(DATEDIFF(Second,SendTime,OverTime) )AS orderusetime from custorder LEFT JOIN dbo.OrderDeliver ON dbo.OrderDeliver.OrderId = dbo.Custorder.orderid where " + where + "  AND OverTime > '2000-1-1' AND DeliveryTime > '2000-1-1'   group by CONVERT(varchar(100), OrderDateTime, 23)"; break;
                case "mounth": strSql = "select datename(year,OrderDateTime)+'-'+datename(month,OrderDateTime) as CountKey   ,count(1) as CountIntValue,sum (case when deliversiteid =10  then (1) else 0   end)  as deliversiteid     ,Sum(OrderSums) as CountDecimalPrice ,Sum(ReveInt2) as ReveInt2,Sum(shopdiscountmoney) as CountSendFee,SUM(DATEDIFF(Second,DeliveryTime,OverTime) )AS sendtime,SUM(DATEDIFF(Second,SendTime,OverTime) )AS orderusetime from custorder LEFT JOIN dbo.OrderDeliver ON dbo.OrderDeliver.OrderId = dbo.Custorder.orderid where " + where + "  AND OverTime > '2000-1-1' AND DeliveryTime > '2000-1-1'  group by datename(year,OrderDateTime)+'-'+datename(month,OrderDateTime)"; break;

            }

            HJlog.toLog(strSql);


            OrderCountInfo info = new OrderCountInfo();

            using (SqlDataReader dr = SQLHelper.ExecuteReader(CommandType.Text, strSql, null))
            {
                while (dr.Read())
                {
                    info = new OrderCountInfo();

                    info.CountKey = HJConvert.ToString(dr["CountKey"]);
                    info.CountIntValue = HJConvert.ToInt32(dr["CountIntValue"]);
                    info.CountDecimalValue = HJConvert.ToDecimal(dr["CountDecimalPrice"]);
                    info.CountSendFee = HJConvert.ToInt32(dr["CountSendFee"]);

                    info.sendtime = HJConvert.ToInt32(dr["sendtime"]);
                    info.damagecount = HJConvert.ToInt32(dr["ReveInt2"]);


                    info.orderusetime = HJConvert.ToInt32(dr["orderusetime"]);

                    info.perordersendfee = "--";
                    if (info.CountIntValue > 0)
                    {
                        info.perordersendfee = (info.CountSendFee / info.CountIntValue).ToString("0.0");
                        info.perorderUsetime = Utils.second2MinAndSecond((info.orderusetime * 1.0 / info.CountIntValue));
                        info.perorderSendtime = Utils.second2MinAndSecond((info.sendtime * 1.0 / info.CountIntValue));

                        info.perorderprice = (info.CountDecimalValue / info.CountIntValue).ToString("0.0");

                    }

                    info.timeout = HJConvert.ToInt32(dr["deliversiteid"]);

                    list.Add(info);
                }
            }

            return list;
        }


        /// <summary>
        /// Type:1 小时统计 2 日统计 3 周统计 4 月统计 5 年统计
        /// Year 需要统计的年份 Month需要统计的月份 Day需要统计的日 
        /// 小时统计需要Year Month Day
        /// 日统计需要Year Month 
        /// 月统计需要 Year
        /// </summary>
        /// <param name="Type"></param>
        /// <returns></returns>
        public IList<OrderCountInfo> GetOrderCount(int Type, string Year, string Month, string Day, string where)
        {
            IList<OrderCountInfo> list = new List<OrderCountInfo>();

            string strSql = "";

            switch (Type)
            {
                case 1: strSql = "select datepart(hh,OrderDateTime) as CountKey,count(orderid) as CountIntValue,Sum(OrderSums) as CountDecimalPrice,Sum(shopdiscountmoney) as CountSendFee,(select sum(OrderSums) from custorder where togoid = 1 and year(OrderDateTime) = '" + Year + "' and month(OrderDateTime)='" + Month + "'  and day(OrderDateTime)='" + Day + "'  and " + where + "    group by datepart(hh,OrderDateTime)) as CountDrinkPrice from custorder where year(OrderDateTime) = '" + Year + "' and month(OrderDateTime)='" + Month + "'  and day(OrderDateTime)='" + Day + "'  and " + where + "    group by datepart(hh,OrderDateTime)"; break;
                case 2: strSql = "select day(OrderDateTime) as CountKey,count(orderid) as CountIntValue,Sum(OrderSums) as CountDecimalPrice,Sum(shopdiscountmoney) as CountSendFee,(select top 1 sum(OrderSums) from custorder where togoid=1 and year(OrderDateTime) = '" + Year + "' and month(OrderDateTime)='" + Month + "'  and " + where + "    group by day(OrderDateTime)) as CountDrinkPrice from custorder where year(OrderDateTime) = '" + Year + "' and month(OrderDateTime)='" + Month + "'  and " + where + "    group by day(OrderDateTime)"; break;
                case 3: strSql = ""; break;
                case 4: strSql = "select month(OrderDateTime) as CountKey,count(orderid) as CountIntValue,Sum(OrderSums) as CountDecimalPrice,Sum(shopdiscountmoney) as CountSendFee,(select top 1 sum(OrderSums) from custorder where togoid = 1 and year(OrderDateTime) = '" + Year + "'  and " + where + "  group by month(OrderDateTime)) as CountDrinkPrice from custorder where year(OrderDateTime) = '" + Year + "'  and " + where + "   group by month(OrderDateTime)"; break;
                case 5: strSql = ""; break;
                case 7: strSql = "select datepart(hh,OrderDateTime) as CountKey,count(orderid) as CountIntValue,Sum(OrderSums) as CountDecimalPrice,Sum(shopdiscountmoney) as CountSendFee from custorder where " + where + "    group by datepart(hh,OrderDateTime)"; break;

            }


            OrderCountInfo info = new OrderCountInfo();

            using (SqlDataReader dr = SQLHelper.ExecuteReader(CommandType.Text, strSql, null))
            {
                while (dr.Read())
                {
                    info = new OrderCountInfo();

                    info.CountKey = HJConvert.ToString(dr["CountKey"]);
                    info.CountIntValue = HJConvert.ToInt32(dr["CountIntValue"]);
                    info.CountDecimalValue = HJConvert.ToDecimal(dr["CountDecimalPrice"]);
                    info.CountSendFee = HJConvert.ToInt32(dr["CountSendFee"]);

                    list.Add(info);
                }
            }

            return list;
        }

        /// <summary>
        /// 获取报表记录总是和总金额(getmoney页面)
        /// </summary>
        /// <param name="togoNum"></param>
        /// <param name="time1"></param>
        /// <param name="time2"></param>
        /// <returns></returns>
        public CustorderInfo GetCountAndTotal1(string where)
        {

            string sql = "select sum(OrderSums) as  OrderTotal,Count(*) as OrderCount,sum(Commission) as Commission,sum(SendFee) as SendFee,sum(shopdiscountmoney) as shopdiscountmoney,sum(DeliverGetSendFee) as DeliverGetSendFee,sum(delivergetfee_base) as delivergetfee_base,sum(Advance) as Advance,sum(cardpay) as cardpay,sum (case when deliversiteid =0  then (shopdiscountmoney) else 0   end)  as nopaymoeny,sum (0)  as profit";


            sql += " , sum (case when settlecycle = 0  then(shopdiscountmoney) else  0  end)  as paynow_shopdiscountmoney";


            sql += " , sum (case when settlecycle = 1 or systemuserid=1   then(0) else shopdiscountmoney   end)  as delivergivemoney";

            sql += " , sum (case when  systemuserid=1  then(1) else 0   end)  as SystemUserId";
            sql += " from Custorder with(nolock) where " + where;

            CustorderInfo model = null;
            using (SqlDataReader dr = SQLHelper.ExecuteReader(CommandType.Text, sql, null))
            {
                if (dr.Read())
                {
                    model = new CustorderInfo();
                    model.OrderCount = HJConvert.ToInt32(dr["OrderCount"]);
                    model.OrderTotal = HJConvert.ToDecimal(dr["OrderTotal"]);
                    model.shopdiscountmoney = HJConvert.ToDecimal(dr["shopdiscountmoney"]);

                    model.paynow_shopdiscountmoney = HJConvert.ToDecimal(dr["paynow_shopdiscountmoney"]);

                    model.weeknow_shopdiscountmoney = model.shopdiscountmoney - model.paynow_shopdiscountmoney;


                    model.cardpay = HJConvert.ToDecimal(dr["cardpay"]);

                    model.SendFee = HJConvert.ToDecimal(dr["SendFee"]);


                    model.Advance = HJConvert.ToDecimal(dr["Advance"]);
                    model.CallPhoneNo = HJConvert.ToDecimal(dr["nopaymoeny"]).ToString();
                    model.OldPrice = HJConvert.ToDecimal(dr["profit"]);
                    model.delivergivemoney = HJConvert.ToDecimal(dr["delivergivemoney"]);
                    model.Commission = HJConvert.ToDecimal(dr["Commission"]);
                    model.SystemUserId = HJConvert.ToInt32(dr["SystemUserId"]);

                    model.DeliverGetSendFee = HJConvert.ToDecimal(dr["DeliverGetSendFee"]);
                    model.delivergetfee_base = HJConvert.ToDecimal(dr["delivergetfee_base"]);

                }
            }
            return model;
        }


        /// <summary>
        /// 统计配送时间
        /// </summary>
        /// <param name="togoNum"></param>
        /// <param name="time1"></param>
        /// <param name="time2"></param>
        /// <returns></returns>
        public CustorderInfo GetSumSendTime(string where)
        {
            string sql = "select sum(OrderSums) as  OrderTotal,Count(*) as OrderCount,sum(shopdiscountmoney) as shopdiscountmoney,sum(Commission) as Commission,sum(Advance) as Advance,sum(cardpay) as cardpay,sum (case when deliversiteid =0  then (shopdiscountmoney) else 0   end)  as nopaymoeny,sum (case when OrderSums >Advance and Advance > 0  then (OrderSums - Advance) else 0   end)  as profit";
            sql += " , sum (case when settlecycle = 1 or systemuserid=1   then(0) else shopdiscountmoney   end)  as delivergivemoney,avg(DATEDIFF(Second,DeliveryTime,OverTime) )AS sendtime,avg(DATEDIFF(Second,sendtime,OverTime) )AS orderusetime ";
            sql += " , sum (case when  deliversiteid=10  then(1) else 0   end)  as deliversiteid";
            sql += " , sum (case when  systemuserid=1  then(1) else 0   end)  as SystemUserId,sum(delivergetfee_base) as delivergetfee_base,sum(DeliverGetSendFee) as DeliverGetSendFee";
            sql += " from Custorder LEFT JOIN dbo.OrderDeliver ON dbo.OrderDeliver.OrderId = dbo.Custorder.orderid where " + where + " and  OverTime > '2000-1-1' AND DeliveryTime > '2000-1-1'";

            //HJlog.toLog(sql);

            CustorderInfo model = null;
            using (SqlDataReader dr = SQLHelper.ExecuteReader(CommandType.Text, sql, null))
            {
                if (dr.Read())
                {
                    model = new CustorderInfo();
                    model.OrderCount = HJConvert.ToInt32(dr["OrderCount"]);
                    model.OrderTotal = HJConvert.ToDecimal(dr["OrderTotal"]);
                    model.shopdiscountmoney = HJConvert.ToDecimal(dr["shopdiscountmoney"]);
                    model.cardpay = HJConvert.ToDecimal(dr["cardpay"]);
                    model.Advance = HJConvert.ToDecimal(dr["Advance"]);
                    model.CallPhoneNo = HJConvert.ToDecimal(dr["nopaymoeny"]).ToString();
                    model.OldPrice = HJConvert.ToDecimal(dr["profit"]);
                    model.delivergivemoney = HJConvert.ToDecimal(dr["delivergivemoney"]);
                    model.delflag = HJConvert.ToInt32(dr["SendTime"]);
                    model.SystemUserId = HJConvert.ToInt32(dr["SystemUserId"]);
                    model.deliversiteid = HJConvert.ToInt32(dr["deliversiteid"]);

                    model.Commission = HJConvert.ToDecimal(dr["Commission"]);

                    model.delivergetfee_base = HJConvert.ToDecimal(dr["delivergetfee_base"]);

                    model.DeliverGetSendFee = HJConvert.ToDecimal(dr["DeliverGetSendFee"]);

                    model.UserId = HJConvert.ToInt32(dr["orderusetime"]);
                }
            }
            return model;
        }

        /// <summary>
        /// 每日销售金额统计
        /// </summary>
        /// <param name="SDate"></param>
        /// <param name="EDate"></param>
        /// <returns></returns>
        public IList<ShopOrderCountInfo> GetShopDayOrderCount(string TogoId, DateTime SDate, DateTime EDate)
        {
            //如查询日期是2011-5-30 日 则 两个参数分别是 2011-5-30  2011-5-31
            //如查询日期是2011-5-25 -- 2011-5-30 则 两个参数分别是 2011-5-25  2011-5-31
            //select sum(OrderSums) as OrderSum, OrderDateTime from Custorder where TogoId=1 and OrderDateTime < '2011-5-28 00:00:00' and  OrderDateTime > '2011-5-24 00:00:00' group by day(OrderDateTime)

            string strSql = "select sum(OrderSums) as CountDecimalValue,count(Unid) as OrderCount,  OrderDateTime from custorder where TogoId=" + TogoId + " and OrderDateTime < '" + EDate.AddDays(1).ToString("yyyy-MM-dd") + " 00:00:00' and  OrderDateTime > '" + SDate.ToString("yyyy-MM-dd") + " 00:00:00' group by day(OrderDateTime)";

            IList<ShopOrderCountInfo> list = new List<ShopOrderCountInfo>();

            ShopOrderCountInfo info = new ShopOrderCountInfo();

            using (SqlDataReader dr = SQLHelper.ExecuteReader(CommandType.Text, strSql, null))
            {
                while (dr.Read())
                {
                    info = new ShopOrderCountInfo();

                    info.CountDecimalValue = HJConvert.ToDecimal(dr["CountDecimalValue"]);
                    info.OrderCount = HJConvert.ToInt32(dr["OrderCount"]);
                    info.OrderDate = HJConvert.ToDateTime(dr["OrderDateTime"]);

                    list.Add(info);
                }
            }

            return list;
        }

        /// <summary>
        /// 订单加分,判断是不是一个订单  此方法需要改进以及测试 2011.6.10
        /// </summary>
        /// <param name="orerid"></param>
        /// <returns></returns>
        public int AddPoint(string orerid)
        {
            CustorderInfo model = new Custorder().GetModel(orerid);

            WebBasicInfo mysetpoint = new WebBasic().GetModel(42);
            int ratpoint = Convert.ToInt32(mysetpoint.Value);
            //积分计算公式： 订单总金额*积分倍数
            int point = Convert.ToInt32(Convert.ToInt32(model.OrderSums) * ratpoint);

            //int point = Convert.ToInt32(model.OrderSums);
            SqlParameter[] Para =
            {
                new SqlParameter("@userid",SqlDbType.Int,5),
                new SqlParameter("@addpoint",SqlDbType.Int , 5),
                new SqlParameter("@orderid",SqlDbType.VarChar,20),
            };
            Para[0].Value = model.UserId;
            Para[1].Value = point;
            Para[2].Value = orerid;

            SQLHelper.ExecuteNonQuery(CommandType.StoredProcedure, "addOrderPoint", Para);


            //如果这个配送员手上没有配送员的订单，状态设置待岗
            string sql = " OrderStatus in (1,2,7) and  deliverid = " + model.deliverid;
            if (GetCount(sql) == 0)
            {
                sql = "UPDATE dbo.Deliver SET workstate = 10 where dataid = " + model.deliverid;
                SQLHelper.excutesql(sql);
            }
            return 1;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="model"></param>
        public void checkRecommend(CustorderInfo model) {

        }


        /// <summary>
        /// 下订单
        /// </summary>
        /// <param name="list"></param>
        /// <param name="address"></param>
        /// <returns></returns>
        public IList<ROrderinfo> SubmitOrder(IList<Hangjing.Model.ETogoShoppingCart> list, CustorderInfo address)
        {
            IList<ROrderinfo> my_list = new List<ROrderinfo>();
            ArrayList cmdtextlist = new ArrayList();

            ArrayList paraslist = new ArrayList();

            string sign = HjNetHelper.GetTime();
            int i = 0, j = 0;
            for (i = 0; i < list.Count; i++)//点了几个商家.就是会有多少的订单.
            {
                int delflag = 0;
                int orderstatus = 2;//审核通过
                if (address.fromweb == "7" && address.paymode == 5)
                {
                    orderstatus = 1;//用户提交的都是未审核的
                    delflag = 1;//支付完成后，设置成可见
                }

                //根据折扣信息，来计算总价
                Points togo = new Points();
                PointsInfo togomodel = togo.GetModel(list[i].TogoId);

                decimal delivergetfee = 0;
                decimal delivergetfee_base = curDeliverGetSendFee(togomodel, address, out delivergetfee);
                if (delivergetfee_base == 0)
                {
                    delivergetfee_base = address.shopdiscountmoney;
                }


                ROrderinfo m = new ROrderinfo();
                m.Currentprice = address.OrderSums;
                m.allprice = address.OrderSums;
                m.accountpay = 0;
                m.sendfee = list[i].sendfree;
                m.togoid = list[i].TogoId;
                m.WeiXxinOpenID = togomodel.PosAddr;
                m.cardpay = 0;
                m.latlng = list[i].latlng;
                m.sentorg = togomodel.sentorg;
                m.cityid = togomodel.cityid;
                m.groupid = togomodel.IsCallCenter;

                my_list.Add(m);

                StringBuilder strSql = new StringBuilder();



                strSql.Append("insert into custorder(");
                strSql.Append("InUse,OrderDateTime,OrderChecker,OrderStatus,OrderRcver,OrderComm,OrderAddress,AddressText,OrderAddrEx,OrderAttach,OrderSums,Sender,SendTime,CallPhoneNo,P2Sign,SendFee,paymode,paytime,paymoney,paystate,SetStateTime, UserId, TogoId,orderid,fromweb,CityID,deliversiteid,ReveVar1,ReveVar2,OldPrice,ReveInt1,ReveInt2,oldstatus,shopdiscountmoney,tempcode,cardpay,cardids,pointrat,Commission,settlecycle,Advance,Mergeorderremark,SystemUserId,deliverheaderid,IsShopSet,ReveDate1,delflag,Packagefee,DeliverGetSendFee,delivergetfee_base)");
                strSql.Append(" values (");
                strSql.Append("@InUse,@OrderDateTime,@OrderChecker,@OrderStatus,@OrderRcver,@OrderComm,@OrderAddress,@AddressText,@OrderAddrEx,@OrderAttach,@OrderSums,@Sender,@SendTime,@CallPhoneNo,@P2Sign,@SendFee,@paymode,@paytime,@paymoney,@paystate,@SetStateTime,@UserId, @TogoId,@orderid,@fromweb,@CityID,@deliversiteid,@ReveVar1,@ReveVar2,@OldPrice,@ReveInt1,@ReveInt2,@oldstatus,@shopdiscountmoney,@tempcode,@cardpay,@cardids,@pointrat,@Commission,@settlecycle,@Advance,@Mergeorderremark,@SystemUserId,@deliverheaderid,@IsShopSet,@ReveDate1,@delflag,@Packagefee," + delivergetfee + "," + delivergetfee_base + ");");
                strSql.Append(";select @@IDENTITY");
                SqlParameter[] parameters =
                {
                    new SqlParameter("@InUse", SqlDbType.VarChar,50),
                    new SqlParameter("@OrderDateTime", SqlDbType.DateTime),
                    new SqlParameter("@OrderChecker", SqlDbType.Int,4),
                    new SqlParameter("@OrderStatus", SqlDbType.Int,4),
                    new SqlParameter("@OrderRcver", SqlDbType.VarChar,50),
                    new SqlParameter("@OrderComm", SqlDbType.VarChar,50),
                    new SqlParameter("@OrderAddress", SqlDbType.VarChar,50),
                    new SqlParameter("@AddressText", SqlDbType.VarChar,300),
                    new SqlParameter("@OrderAddrEx", SqlDbType.VarChar,300),
                    new SqlParameter("@OrderAttach", SqlDbType.VarChar,512),
                    new SqlParameter("@OrderSums", SqlDbType.Decimal,9),
                    new SqlParameter("@Sender", SqlDbType.VarChar,256),
                    new SqlParameter("@SendTime", SqlDbType.DateTime),
                    new SqlParameter("@CallPhoneNo", SqlDbType.VarChar,256),
                    new SqlParameter("@P2Sign", SqlDbType.VarChar,256),
                    new SqlParameter("@SendFee", SqlDbType.Decimal,9),
                    new SqlParameter("@paymode", SqlDbType.Int,4),
                    new SqlParameter("@paytime", SqlDbType.DateTime),
                    new SqlParameter("@paymoney", SqlDbType.Decimal,9),
                    new SqlParameter("@paystate", SqlDbType.Int,4),
                    new SqlParameter("@SetStateTime", SqlDbType.DateTime),
                    new SqlParameter("@UserId", SqlDbType.Int,11),
                    new SqlParameter("@TogoId", SqlDbType.Int,11),
                    new SqlParameter("@orderid", SqlDbType.VarChar,50),
                    new SqlParameter("@fromweb", SqlDbType.VarChar,50),
                    new SqlParameter("@CityID",SqlDbType.Int,4),
                    new SqlParameter("@deliversiteid",SqlDbType.Int,4),
                    new SqlParameter("@ReveVar1",SqlDbType.VarChar,256),
                    new SqlParameter("@ReveVar2",SqlDbType.VarChar,256),
                    new SqlParameter("@OldPrice",SqlDbType.Decimal,9),
                    new SqlParameter("@ReveInt1", SqlDbType.Int,11),
                    new SqlParameter("@ReveInt2", SqlDbType.Int,11),
                    new SqlParameter("@oldstatus", SqlDbType.Int,4),
                    new SqlParameter("@shopdiscountmoney", SqlDbType.Decimal,9),
                    new SqlParameter("@tempcode",SqlDbType.VarChar,256),
                    new SqlParameter("@cardpay",SqlDbType.Decimal,9),
                    new SqlParameter("@cardids",SqlDbType.VarChar,256),
                    new SqlParameter("@pointrat", SqlDbType.Int,11),
                    new SqlParameter("@Commission", SqlDbType.Decimal,9) ,
                    new SqlParameter("@settlecycle", SqlDbType.Int,4) ,
                    new SqlParameter("@Advance", SqlDbType.Decimal,9) ,
                    new SqlParameter("@Mergeorderremark", SqlDbType.VarChar,256),
                    new SqlParameter("@SystemUserId", SqlDbType.Int,4) ,
                    new SqlParameter("@deliverheaderid", SqlDbType.Int,4) ,
                    new SqlParameter("@IsShopSet", SqlDbType.Int),
                    new SqlParameter("@ReveDate1", SqlDbType.DateTime),
                    new SqlParameter("@delflag", SqlDbType.Int),
                    new SqlParameter("@Packagefee", SqlDbType.Decimal,9) ,

               };
                parameters[0].Value = togomodel.PHead;
                parameters[1].Value = DateTime.Now;
                parameters[2].Value = 0;//前台
                parameters[3].Value = orderstatus;
                parameters[4].Value = address.OrderRcver;
                parameters[5].Value = address.OrderComm;
                parameters[6].Value = address.OrderAddress;
                parameters[7].Value = address.AddressText;
                parameters[8].Value = "";
                parameters[9].Value = address.OrderAttach;
                parameters[10].Value = address.OrderSums;
                parameters[11].Value = "";
                parameters[12].Value = address.SendTime;
                parameters[13].Value = "";
                parameters[14].Value = "";
                parameters[15].Value = address.SendFee;
                parameters[16].Value = address.paymode;
                parameters[17].Value = Convert.ToDateTime("1900-01-01");
                parameters[18].Value = 0;
                parameters[19].Value = 0;
                parameters[20].Value = DateTime.Now;
                parameters[21].Value = address.UserId;
                parameters[22].Value = togomodel.Unid;
                parameters[23].Value = "";
                parameters[24].Value = address.fromweb;
                parameters[25].Value = togomodel.cityid;
                parameters[26].Value = 0;
                parameters[27].Value = togomodel.sentorg;
                parameters[28].Value = list[i].latlng;
                parameters[29].Value = address.OrderSums;
                parameters[30].Value = 0;
                parameters[31].Value = address.ReveInt2;
                parameters[32].Value = togomodel.PEnd;
                parameters[33].Value = address.shopdiscountmoney;
                parameters[34].Value = address.tempcode;
                parameters[35].Value = address.cardpay;
                parameters[36].Value = address.cardids;
                parameters[37].Value = 0;
                parameters[38].Value = address.Commission;
                parameters[39].Value = address.settlecycle;
                parameters[40].Value = address.Advance;
                parameters[41].Value = address.Mergeorderremark;
                parameters[42].Value = address.SystemUserId;
                parameters[43].Value = togomodel.IsCallCenter;

                parameters[44].Value = address.IsShopSet;
                parameters[45].Value = address.ReveDate1;
                parameters[46].Value = delflag;
                parameters[47].Value = address.Packagefee;




                //订单中编辑
                int unid = Convert.ToInt32(SQLHelper.ExecuteScalar(CommandType.Text, strSql.ToString(), parameters));
                string orderid = HjNetHelper.GetTime() + unid.ToString("00000");

                if (address.fromweb == "5" || address.fromweb == "10")
                {
                    orderid = address.orderid;
                }

                if (togomodel.Unid > 0)
                {
                    TogoAddMoneyLogInfo shopmoney = new TogoAddMoneyLogInfo();
                    shopmoney.UserId = togomodel.Unid;
                    shopmoney.AddMoney = -address.shopdiscountmoney;
                    shopmoney.Inve2 = orderid;
                    ShopAccount(shopmoney);

                }



                m.Orderid = orderid;

                //修改商家表里的POP（销售量）字段
                StringBuilder strSql3 = new StringBuilder();


                strSql3.Append("insert into OrderStep(");
                strSql3.Append("stepcode,orderid,title,subtitle,addtime,deliverid,reveint1,reveint2,revevar1,revevar2");
                strSql3.Append(") values (");
                strSql3.Append("0,'" + orderid + "','订单已经提交','',getdate(),0,0,0,'',''");
                strSql3.Append(");");



                strSql3.Append("update Custorder set orderid = '" + orderid + "' where unid = " + unid + ";");
                SqlParameter[] param = {
                  new SqlParameter("@Unid", SqlDbType.Int,4)
                };
                param[0].Value = list[i].TogoId;

                cmdtextlist.Add(strSql3.ToString());
                paraslist.Add(param);

                for (j = 0; j < list[i].ItemList.Count; j++)//每个订单中的餐品
                {
                    StringBuilder strSql2 = new StringBuilder();
                    strSql2.Append("insert into Foodlist(");
                    strSql2.Append("InUse,COUnid,FoodUnid,FoodPrice,FCounts,Remark,OldPrice,TogoId,adddate,orderid,fooname,packagefee)");
                    strSql2.Append(" values (");
                    strSql2.Append("@InUse,@COUnid,@FoodUnid,@FoodPrice,@FCounts,@Remark,@OldPrice,@TogoId,@adddate,@orderid,@fooname,@packagefee);");
                    strSql2.Append("UPDATE dbo.Foodinfo SET Remains = Remains - @FCounts WHERE unid = @FoodUnid;");



                    SqlParameter[] parameters2 =
                    {
                        new SqlParameter("@InUse", SqlDbType.VarChar,10),
                        new SqlParameter("@COUnid", SqlDbType.Int,4),
                        new SqlParameter("@FoodUnid", SqlDbType.Int,4),
                        new SqlParameter("@FoodPrice", SqlDbType.Decimal,9),
                        new SqlParameter("@FCounts", SqlDbType.Int,4),
                        new SqlParameter("@Remark", SqlDbType.VarChar,50),
                        new SqlParameter("@OldPrice", SqlDbType.Decimal,9),
                        new SqlParameter("@TogoId", SqlDbType.Int,4),
                        new SqlParameter("@adddate", SqlDbType.DateTime),
                        new SqlParameter("@orderid", SqlDbType.VarChar,50),
                        new SqlParameter("@fooname", SqlDbType.VarChar,200),
                        new SqlParameter("@packagefee", SqlDbType.Decimal,9),


                    };
                    parameters2[0].Value = "Y";
                    parameters2[1].Value = unid;
                    parameters2[2].Value = list[i].ItemList[j].PId;
                    parameters2[3].Value = list[i].ItemList[j].Currentprice;
                    parameters2[4].Value = list[i].ItemList[j].PNum;
                    parameters2[5].Value = list[i].ItemList[j].Remark;
                    parameters2[6].Value = list[i].ItemList[j].PPrice;
                    parameters2[7].Value = togomodel.Unid;
                    parameters2[8].Value = DateTime.Now;
                    parameters2[9].Value = orderid;
                    parameters2[10].Value = list[i].ItemList[j].PName;
                    parameters2[11].Value = list[i].ItemList[j].packagefee;

                    cmdtextlist.Add(strSql2.ToString());
                    paraslist.Add(parameters2);
                }


            }
            bool flag = SQLHelper.ExecuteSqlTran(CommandType.Text, cmdtextlist, paraslist);
            if (flag)
            {
                // AutoDispatch(my_list); 
                return my_list;
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 下订单
        /// </summary>
        /// <param name="list"></param>
        /// <param name="address"></param>
        /// <returns></returns>
        public int AddTBOrder(CustorderInfo model)
        {
            IList<ROrderinfo> my_list = new List<ROrderinfo>();


            ArrayList paraslist = new ArrayList();


            decimal delivergetfee = 0;
            decimal delivergetfee_base = 0;



            if (model.TogoId == 0)
            {
                delivergetfee_base = curDeliverGetSendFee(model.cityid, model);
                delivergetfee = delivergetfee_base;
            }
            else
            {
                delivergetfee_base = curDeliverGetSendFee(new Points().GetModel(model.TogoId), model, out delivergetfee);

                if (delivergetfee_base == 0)
                {
                    delivergetfee_base = model.shopdiscountmoney;
                }
            }

            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into custorder(");
            strSql.Append("InUse,OrderDateTime,OrderChecker,OrderStatus,OrderRcver,OrderComm,OrderAddress,AddressText,OrderAddrEx,OrderAttach,OrderSums,Sender,SendTime,CallPhoneNo,P2Sign,SendFee,paymode,paytime,paymoney,paystate,SetStateTime, UserId, TogoId,orderid,fromweb,CityID,deliversiteid,ReveVar1,ReveVar2,OldPrice,ReveInt1,ReveInt2,oldstatus,shopdiscountmoney,tempcode,cardpay,cardids,pointrat,IsShopSet,ReveDate1,Commission,settlecycle,Advance,Mergeorderremark,SystemUserId,deliverheaderid,shoptip,shopauthuser,delflag,DeliverGetSendFee,delivergetfee_base)");
            strSql.Append(" values (");
            strSql.Append("@InUse,@OrderDateTime,@OrderChecker,@OrderStatus,@OrderRcver,@OrderComm,@OrderAddress,@AddressText,@OrderAddrEx,@OrderAttach,@OrderSums,@Sender,@SendTime,@CallPhoneNo,@P2Sign,@SendFee,@paymode,@paytime,@paymoney,@paystate,@SetStateTime,@UserId, @TogoId,@orderid,@fromweb,@CityID,@deliversiteid,@ReveVar1,@ReveVar2,@OldPrice,@ReveInt1,@ReveInt2,@oldstatus,@shopdiscountmoney,@tempcode,@cardpay,@cardids,@pointrat,@IsShopSet,@ReveDate1,@Commission,@settlecycle,@Advance,@Mergeorderremark,@SystemUserId,@deliverheaderid,@shoptip,@shopauthuser,@delflag," + delivergetfee + "," + delivergetfee_base + ");");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters =
            {
                new SqlParameter("@InUse", SqlDbType.VarChar,10),
                new SqlParameter("@OrderDateTime", SqlDbType.DateTime),
                new SqlParameter("@OrderChecker", SqlDbType.Int,4),
                new SqlParameter("@OrderStatus", SqlDbType.Int,4),
                new SqlParameter("@OrderRcver", SqlDbType.VarChar,50),
                new SqlParameter("@OrderComm", SqlDbType.VarChar,50),
                new SqlParameter("@OrderAddress", SqlDbType.VarChar,50),
                new SqlParameter("@AddressText", SqlDbType.VarChar,200),
                new SqlParameter("@OrderAddrEx", SqlDbType.VarChar,300),
                new SqlParameter("@OrderAttach", SqlDbType.VarChar,512),
                new SqlParameter("@OrderSums", SqlDbType.Decimal,9),
                new SqlParameter("@Sender", SqlDbType.VarChar,256),
                new SqlParameter("@SendTime", SqlDbType.DateTime),
                new SqlParameter("@CallPhoneNo", SqlDbType.VarChar,256),
                new SqlParameter("@P2Sign", SqlDbType.VarChar,256),
                new SqlParameter("@SendFee", SqlDbType.Decimal,9),
                new SqlParameter("@paymode", SqlDbType.Int,4),
                new SqlParameter("@paytime", SqlDbType.DateTime),
                new SqlParameter("@paymoney", SqlDbType.Decimal,9),
                new SqlParameter("@paystate", SqlDbType.Int,4),
                new SqlParameter("@SetStateTime", SqlDbType.DateTime),
                new SqlParameter("@UserId", SqlDbType.Int,11),
                new SqlParameter("@TogoId", SqlDbType.Int,11),
                new SqlParameter("@orderid", SqlDbType.VarChar,50),
                new SqlParameter("@fromweb", SqlDbType.VarChar,50),
                new SqlParameter("@CityID",SqlDbType.Int,4),
                new SqlParameter("@deliversiteid",SqlDbType.Int,4),
                new SqlParameter("@ReveVar1",SqlDbType.VarChar,256),
                new SqlParameter("@ReveVar2",SqlDbType.VarChar,256),
                new SqlParameter("@OldPrice",SqlDbType.Decimal,9),
                new SqlParameter("@ReveInt1", SqlDbType.Int,11),
                new SqlParameter("@ReveInt2", SqlDbType.Int,11),
                new SqlParameter("@oldstatus", SqlDbType.Int,4),
                new SqlParameter("@shopdiscountmoney", SqlDbType.Decimal,9),
                new SqlParameter("@tempcode",SqlDbType.VarChar,256),
                new SqlParameter("@cardpay",SqlDbType.Decimal,9),
                new SqlParameter("@cardids",SqlDbType.VarChar,256),
                new SqlParameter("@pointrat", SqlDbType.Int,11),
                new SqlParameter("@IsShopSet", SqlDbType.Int),
                new SqlParameter("@ReveDate1", SqlDbType.DateTime),
                new SqlParameter("@Commission", SqlDbType.Decimal,9) ,
                new SqlParameter("@settlecycle", SqlDbType.Int,4) ,
                new SqlParameter("@Advance", SqlDbType.Decimal,9) ,
                new SqlParameter("@Mergeorderremark", SqlDbType.VarChar,256),
                new SqlParameter("@SystemUserId", SqlDbType.Int,4),
                new SqlParameter("@deliverheaderid", SqlDbType.Int,4),
                new SqlParameter("@shoptip", SqlDbType.Decimal,9) ,
                new SqlParameter("@shopauthuser",SqlDbType.VarChar,256),
                new SqlParameter("@delflag", SqlDbType.Int,4),
            };

            if (model.shopauthuser == null)
            {
                model.shopauthuser = "";
            }
            if (model.OrderAddrEx == null)
            {
                model.OrderAddrEx = "";
            }


            int delflag = 0;
            int orderstatus = 2;
            if (model.fromweb == "7")
            {
                orderstatus = 1;//用户提交的都是未审核的
                delflag = 1;//支付完成后，设置成可见
            }

            if (model.orderid == null)
            {
                model.orderid = "";
            }


            parameters[0].Value = "";
            parameters[1].Value = DateTime.Now;
            parameters[2].Value = 0;
            parameters[3].Value = orderstatus;
            parameters[4].Value = model.OrderRcver;
            parameters[5].Value = model.OrderComm;
            parameters[6].Value = model.OrderAddress;
            parameters[7].Value = model.AddressText;
            parameters[8].Value = model.OrderAddrEx;
            parameters[9].Value = model.OrderAttach;
            parameters[10].Value = model.OrderSums;
            parameters[11].Value = "";
            parameters[12].Value = Convert.ToDateTime(model.SendTime);
            parameters[13].Value = "";
            parameters[14].Value = model.P2Sign;
            parameters[15].Value = model.SendFee;
            parameters[16].Value = model.paymode;
            parameters[17].Value = model.paytime;
            parameters[18].Value = model.paymoney;
            parameters[19].Value = model.paystate;
            parameters[20].Value = DateTime.Now;
            parameters[21].Value = model.UserId;
            parameters[22].Value = model.TogoId;
            parameters[23].Value = model.orderid;
            parameters[24].Value = model.fromweb;
            parameters[25].Value = model.cityid;
            parameters[26].Value = 0;
            parameters[27].Value = model.ReveVar1;
            parameters[28].Value = model.ReveVar2;
            parameters[29].Value = model.OldPrice;
            parameters[30].Value = 0;
            parameters[31].Value = 0;
            parameters[32].Value = model.OldStatus;
            parameters[33].Value = model.shopdiscountmoney;
            parameters[34].Value = "";
            parameters[35].Value = model.cardpay;
            parameters[36].Value = model.cardids;
            parameters[37].Value = 0;
            parameters[38].Value = model.IsShopSet;
            parameters[39].Value = model.ReveDate1;
            parameters[40].Value = model.Commission;
            parameters[41].Value = model.settlecycle;
            parameters[42].Value = model.Advance;
            parameters[43].Value = model.Mergeorderremark;
            parameters[44].Value = model.SystemUserId;
            parameters[45].Value = model.deliverheaderid;
            parameters[46].Value = model.shoptip;
            parameters[47].Value = model.shopauthuser;
            parameters[48].Value = delflag;



            int id = Convert.ToInt32(SQLHelper.ExecuteScalar(CommandType.Text, strSql.ToString(), parameters));
            string orderid = DateTime.Now.ToString("yyMMdd") + id.ToString("000000");
            if (model.TogoId == 0)
            {
                orderid = "e" + DateTime.Now.ToString("yyMMdd") + id.ToString("000000");
            }

            model.orderid = orderid;


            string sql = "UPDATE dbo.Custorder SET orderid = '" + orderid + "' WHERE Unid = " + id + ";";
            strSql = new StringBuilder(sql);
            strSql.Append("insert into OrderStep(");
            strSql.Append("stepcode,orderid,title,subtitle,addtime,deliverid,reveint1,reveint2,revevar1,revevar2");
            strSql.Append(") values (");
            strSql.Append("0,'" + orderid + "','订单已经提交','',getdate(),0,0,0,'',''");
            strSql.Append(");");

            SQLHelper.excutesql(strSql.ToString());

            if (model.TogoId > 0)
            {
                TogoAddMoneyLogInfo shopmoney = new TogoAddMoneyLogInfo();
                shopmoney.UserId = model.TogoId;
                shopmoney.AddMoney = -model.shopdiscountmoney;
                shopmoney.Inve2 = orderid;
                ShopAccount(shopmoney);
            }


            ROrderinfo mm = new ROrderinfo();
            mm.latlng = model.ReveVar2;
            mm.Orderid = orderid;
            mm.cityid = model.cityid;
            mm.sentorg = model.ReveVar1;
            mm.groupid = model.deliverheaderid;

            my_list.Add(mm);

            return id;

        }


        public void ShopAccount(TogoAddMoneyLogInfo info)
        {


            info.Inve1 = 0;
            info.State = 1;
            info.PayType = 7;
            info.PayDate = DateTime.Now;
            info.PayState = 1;
            info.AddDate = DateTime.Now;

            new TogoAddMoneyLog().Add(info);
        }

        /// <summary>
        /// 自动调度
        /// </summary>
        /// <param name="my_list"></param>
        public void AutoDispatch(IList<ROrderinfo> my_list)
        {
            autodispatchconfigInfo config = CacheHelper.Getautodispatchconfig().Where(a => a.reveint2 == my_list[0].groupid).FirstOrDefault();
            if (config != null && config.isopen == 1)
            {
                latlnginfo latlng = new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize<latlnginfo>(my_list[0].latlng);

                IList<DeliverInfo> Nearbydelivers = null;


                foreach (var item in my_list)
                {
                    if (item.sentorg == "0") //只有统一配送的才调度
                    {
                        switch (config.autotype)
                        {
                            case 1: //表示发给所有人
                                {
                                    return; //现在取消这个类型了

                                    //订单表中也保存配送员编号，便于统计,修改订单状态:
                                    string sql = "update Custorder set deliverheaderid=" + item.groupid + ",OrderStatus=7,deliverid=0,sendstate=0,ReveDate2=getdate()  where OrderID='" + item.Orderid + "'";
                                    SQLHelper.excutesql(sql);
                                    NoticeHelper notice = new NoticeHelper(null, my_list[0].groupid.ToString());
                                    notice.send2Group();

                                }
                                break;
                            case 2:
                                {
                                    System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
                                    sw.Start();

                                    Nearbydelivers = new Deliver().GetNearbyList(config.distance, latlng.slat, latlng.slng);
                                    string ids = "";
                                    int delivercount = 0;
                                    if (Nearbydelivers != null)
                                    {
                                        foreach (var deliver in Nearbydelivers)
                                        {
                                            delivercount++;
                                            ids += deliver.DataId + ",";
                                            if (delivercount >= config.reveint1)
                                            {
                                                break;
                                            }
                                        }
                                    }
                                    ids = HJConvert.dellast(ids);
                                    if (ids != "")
                                    {
                                        //订单表中也保存配送员编号，便于统计,修改订单状态:
                                        string sql = "update Custorder set deliverheaderid=" + item.groupid + ",OrderStatus=7,deliverid=0,sendstate=0,ReveDate2=getdate()  where OrderID='" + item.Orderid + "'";
                                        SQLHelper.excutesql(sql);

                                        Hangjing.AppLog.AppLog.Info("自动调度：autotype=2，ids=" + ids);

                                        NoticeHelper notice = new NoticeHelper(null);
                                        notice.send2IDs(ids);

                                    }
                                    else
                                    {
                                        Hangjing.AppLog.AppLog.Info("自动调度：autotype=2，没有找到相关配送员");
                                    }

                                    sw.Stop();


                                    Hangjing.AppLog.AppLog.Info("代码运行时间:" + sw.ElapsedMilliseconds);

                                }
                                break;
                            case 3:
                                {
                                    Nearbydelivers = new Deliver().getNearbyOnlineIdle(config.distance, latlng.slat, latlng.slng);
                                    string ids = "";
                                    int delivercount = 0;
                                    if (Nearbydelivers != null)
                                    {
                                        foreach (var deliver in Nearbydelivers)
                                        {
                                            delivercount++;
                                            ids += deliver.DataId + ",";
                                            if (delivercount >= config.reveint1)
                                            {
                                                break;
                                            }
                                        }
                                    }
                                    ids = HJConvert.dellast(ids);
                                    if (ids != "")
                                    {
                                        //订单表中也保存配送员编号，便于统计,修改订单状态:
                                        string sql = "update Custorder set deliverheaderid=" + item.groupid + ",OrderStatus=7,deliverid=0,sendstate=0,ReveDate2=getdate()  where OrderID='" + item.Orderid + "'";
                                        SQLHelper.excutesql(sql);

                                        Hangjing.AppLog.AppLog.Info("自动调度：autotype=3，ids=" + ids);

                                        NoticeHelper notice = new NoticeHelper(null);
                                        notice.send2IDs(ids);

                                    }
                                    else
                                    {
                                        Hangjing.AppLog.AppLog.Info("自动调度：autotype=3，没有找到相关配送员");
                                    }
                                }

                                break;
                            case 4:
                                {
                                    Nearbydelivers = new Deliver().getNearbyOnlineIdle(config.distance, latlng.slat, latlng.slng);
                                    if (Nearbydelivers == null || Nearbydelivers.Count == 0)
                                    {
                                        Hangjing.AppLog.AppLog.Info("自动调度：autotype=" + config.autotype + "，没有找到相关配送员，" + config.distance + "公里内没有配送员");
                                        return;
                                    }

                                    string pro = "deliver_latestAndisidle";

                                    int did = OutsetOrderDeliver(item.Orderid, latlng.slat, latlng.slng, item.groupid, pro);

                                    if (did > 0)
                                    {
                                        NoticeHelper notice = new NoticeHelper(null, did.ToString(), item.Orderid);
                                        notice.sendOrderToDeliver();
                                    }

                                }

                                break;
                            default:
                                break;
                        }
                    }

                }

            }
        }



        /// <summary>
        /// 商家自动调度  自动派给1公里内距离最近、没单待岗的骑士，0表示没找到，
        /// </summary>
        /// <param name="lat"></param>
        /// <param name="lng"></param>
        /// <param name="orderid"></param>
        /// <returns></returns>
        public int ShopAutoDispatch(string lat, string lng, string orderid, int cityid, int gid)
        {
            string pro = "deliver_shopgetlatestAndisidle";

            SqlParameter[] parameters =
            {
                new SqlParameter("@orderid", SqlDbType.VarChar,50),
                new SqlParameter("@lat", SqlDbType.VarChar,50),
                new SqlParameter("@lng", SqlDbType.VarChar,256),
                new SqlParameter("@did", SqlDbType.Int,4),

                new SqlParameter("@cityid", SqlDbType.Int,4),
                new SqlParameter("@gid", SqlDbType.Int,4),
            };
            parameters[0].Value = orderid;
            parameters[1].Value = lat;
            parameters[2].Value = lng;
            parameters[3].Direction = ParameterDirection.Output;
            parameters[4].Value = cityid;
            parameters[5].Value = gid;

            SQLHelper.ExecuteNonQuery(CommandType.StoredProcedure, pro, parameters);
            int did = HJConvert.ToInt32(parameters[3].Value);
            if (did > 0)
            {
                NoticeHelper notice = new NoticeHelper(null, did.ToString(), orderid);
                notice.sendOrderToDeliver();
            }

            return did;
        }

        /// <summary>
        /// 自动调度订单
        /// </summary>
        /// <param name="orderid"></param>
        /// <param name="slat"></param>
        /// <param name="slng"></param>
        /// <returns>返回 配送员编号</returns>
        public int OutsetOrderDeliver(string orderid, string slat, string slng, int cityid, string pro_name)
        {
            SqlParameter[] parameters =
            {
                new SqlParameter("@orderid", SqlDbType.VarChar,50),
                new SqlParameter("@lat", SqlDbType.VarChar,50),
                new SqlParameter("@lng", SqlDbType.VarChar,256),
                new SqlParameter("@did", SqlDbType.Int,4),
                new SqlParameter("@cityid", SqlDbType.Int,4)
            };
            parameters[0].Value = orderid;
            parameters[1].Value = slat;
            parameters[2].Value = slng;
            parameters[3].Direction = ParameterDirection.Output;
            parameters[4].Value = cityid;


            SQLHelper.ExecuteNonQuery(CommandType.StoredProcedure, pro_name, parameters);
            int did = HJConvert.ToInt32(parameters[3].Value);

            return did;
        }

        /// <summary>
        /// 自动调度订单
        /// </summary>
        /// <param name="orderid"></param>
        /// <param name="slat"></param>
        /// <param name="slng"></param>
        /// <returns>返回 配送员编号</returns>
        public int OutsetOrderDeliver(string orderid, string slat, string slng, int cityid)
        {
            return OutsetOrderDeliver(orderid, slat, slng, cityid, "deliver_latest");
        }

        /// <summary>
        /// 取消订单
        /// 查询表，看本次处理的订单是否已经处理过，如果存在则更新对应业务系统中订单状态为“未传送取消订单”，
        /// 要更新的表iocache的Status字段值为-10 ，custorder的字段值OrderStatus为-10，且要更新本订单中餐品的库存，
        /// 不存在则只对网站更新本订单中对应业务系统中餐品的库存
        /// </summary>
        /// <returns></returns>
        public bool UpdateCallCenterOrder(string orderid, int iocachestatus, int custorderstatus)
        {
            ArrayList cmdtextlist = new ArrayList();

            ArrayList paraslist = new ArrayList();

            StringBuilder strSql = new StringBuilder();

            StringBuilder strSql2 = new StringBuilder();

            strSql.Append("update custorder set OrderStatus=" + custorderstatus.ToString() + " where orderid=@orderid");

            cmdtextlist.Add(strSql.ToString());

            SqlParameter[] parameters =
            {
                new SqlParameter("@orderid", SqlDbType.VarChar,50),
            };
            paraslist.Add(parameters);

            strSql2.Append("update iocache set Status=" + iocachestatus.ToString() + " where orderid=@orderid");

            SqlParameter[] parameters2 =
            {
                new SqlParameter("@orderid", SqlDbType.VarChar,50),
            };

            cmdtextlist.Add(strSql2.ToString());
            paraslist.Add(parameters2);

            bool flag = SQLHelper.ExecuteSqlTran(CommandType.Text, cmdtextlist, paraslist);
            //bool flag = CallCenterMySqlHelper.ExecuteSqlTran(CommandType.Text, cmdtextlist, paraslist);

            return flag;
        }

        /// <summary>
        /// 调度页面获取列表 包含配送信息
        /// </summary>
        /// <param name="pagesize">页尺寸</param>
        /// <param name="pageindex">页索引</param>
        /// <param name="strWhere">查询条件</param>
        /// <param name="orderName">排序字段</param>
        /// <param name="orderType">排序类型，1为降序，0为升序</param>
        /// <returns>图书列表</returns>
        ///此代码由杭景科技代码内部生成器自动生成
        public IList<CustorderInfo> GetListForDelive(int pagesize, int pageindex, string strWhere, string orderName, int orderType)
        {
            IList<CustorderInfo> infos = new List<CustorderInfo>();
            SqlParameter[] parameters =
            {
                new SqlParameter("@pagesize", SqlDbType.Int),
                new SqlParameter("@pageindex", SqlDbType.Int),
                new SqlParameter("@orderfield", SqlDbType.VarChar,200),
                new SqlParameter("@ordertype", SqlDbType.VarChar,50),
                new SqlParameter("@where", SqlDbType.VarChar,4500)
            };
            parameters[0].Value = pagesize;
            parameters[1].Value = pageindex;
            parameters[2].Value = orderName;
            if (orderType == 1)
            {
                parameters[3].Value = "desc";
            }
            else
            {
                parameters[3].Value = "asc";
            }
            parameters[4].Value = strWhere;

            using (SqlDataReader dr = SQLHelper.ExecuteReader(CommandType.StoredProcedure, "DeliverGetOrderListWithAll", parameters))
            {
                while (dr.Read())
                {
                    CustorderInfo model = new CustorderInfo();
                    model.Unid = HJConvert.ToInt32(dr["Unid"]);
                    model.InUse = HJConvert.ToString(dr["InUse"]);
                    model.OrderDateTime = HJConvert.ToDateTime(dr["OrderDateTime"]);
                    model.OrderChecker = HJConvert.ToInt32(dr["OrderChecker"]);
                    model.OrderStatus = HJConvert.ToInt32(dr["OrderStatus"]);
                    model.OrderRcver = HJConvert.ToString(dr["OrderRcver"]);
                    model.OrderComm = HJConvert.ToString(dr["OrderComm"]);
                    model.OrderAddress = HJConvert.ToString(dr["OrderAddress"]);
                    model.AddressText = HJConvert.ToString(dr["AddressText"]);
                    model.OrderAddrEx = HJConvert.ToString(dr["OrderAddrEx"]);
                    model.OrderAttach = HJConvert.ToString(dr["OrderAttach"]);
                    model.OrderSums = HJConvert.ToDecimal(dr["OrderSums"]);
                    model.Sender = HJConvert.ToString(dr["Sender"]);
                    model.SendTime = HJConvert.ToDateTime(dr["SendTime"]);
                    model.CallPhoneNo = HJConvert.ToString(dr["CallPhoneNo"]);
                    model.P2Sign = HJConvert.ToString(dr["P2Sign"]);
                    model.SendFee = HJConvert.ToDecimal(dr["SendFee"]);
                    model.paymode = HJConvert.ToInt32(dr["paymode"]);
                    model.paytime = HJConvert.ToDateTime(dr["paytime"]);
                    model.paymoney = HJConvert.ToDecimal(dr["paymoney"]);
                    model.cardpay = HJConvert.ToDecimal(dr["cardpay"]);
                    model.paystate = HJConvert.ToInt32(dr["paystate"]);
                    model.SetStateTime = HJConvert.ToDateTime(dr["SetStateTime"]);
                    model.UserId = HJConvert.ToInt32(dr["UserId"]);
                    model.TogoId = HJConvert.ToInt32(dr["TogoId"]);
                    model.TogoName = HJConvert.ToString(dr["TogoName"]);
                    model.orderid = HJConvert.ToString(dr["orderid"]);
                    model.SystemUserId = HJConvert.ToInt32(dr["SystemUserId"]);
                    model.OldStatus = HJConvert.ToInt32(dr["OldStatus"]);
                    model.IsShopSet = HJConvert.ToInt32(dr["IsShopSet"]);
                    model.deliversiteid = HJConvert.ToInt32(dr["deliversiteid"]);
                    model.deliverheaderid = HJConvert.ToInt32(dr["deliverheaderid"]);
                    model.deliverid = HJConvert.ToInt32(dr["deliverid"]);
                    model.sendstate = HJConvert.ToInt32(dr["sendstate"]);
                    model.ReveInt1 = HJConvert.ToInt32(dr["ReveInt1"]);
                    model.ReveInt2 = HJConvert.ToInt32(dr["ReveInt2"]);
                    model.ReveVar1 = HJConvert.ToString(dr["ReveVar1"]);
                    model.ReveVar2 = HJConvert.ToString(dr["ReveVar2"]);
                    model.ReveDate1 = HJConvert.ToDateTime(dr["ReveDate1"]);
                    model.ReveDate2 = HJConvert.ToDateTime(dr["ReveDate2"]);
                    model.fromweb = HJConvert.ToString(dr["fromweb"]);
                    model.Mergeorderremark = HJConvert.ToString(dr["Mergeorderremark"]);
                    model.groupname = HJConvert.ToString(dr["groupname"]);


                    //配送信息
                    OrderDeliverInfo dinfo = new OrderDeliverInfo();
                    dinfo.Orderid = HJConvert.ToString(dr["OrderId"]);
                    dinfo.DeliverId = HJConvert.ToInt32(dr["DeliverId"]);
                    dinfo.DeliverName = HJConvert.ToString(dr["DeliverName"]);
                    dinfo.Dispatcher = HJConvert.ToString(dr["Dispatcher"]);
                    dinfo.DispatchTime = HJConvert.ToDateTime(dr["DispatchTime"]);
                    dinfo.DeliveryTime = HJConvert.ToDateTime(dr["DeliveryTime"]);
                    dinfo.OverTime = HJConvert.ToDateTime(dr["OverTime"]);

                    dinfo.ordertimesum = "--";
                    dinfo.OverTimeStr = "--";

                    DateTime senddoor = Convert.ToDateTime("2000-1-1");

                    dinfo.ordertimesum = Utils.second2MinAndSecond((DateTime.Now - model.SendTime).TotalSeconds);

                    if (dinfo.DeliveryTime > senddoor)
                    {
                        if (model.OrderStatus == 3)
                        {
                            dinfo.OverTimeStr = Utils.second2MinAndSecond((dinfo.OverTime - dinfo.DeliveryTime).TotalSeconds);
                            dinfo.ordertimesum = Utils.second2MinAndSecond((dinfo.OverTime - model.SendTime).TotalSeconds);
                        }
                        else
                        {
                            dinfo.OverTimeStr = Utils.second2MinAndSecond((DateTime.Now - dinfo.DeliveryTime).TotalSeconds);

                        }
                    }

                    dinfo.dispatchTimeStr = "--";
                    if (dinfo.DispatchTime > senddoor)
                    {
                        dinfo.dispatchTimeStr = Utils.second2MinAndSecond((DateTime.Now - dinfo.DispatchTime).TotalSeconds);
                    }



                    dinfo.Section = HJConvert.ToString(dr["Section"]);
                    dinfo.Inve1 = HJConvert.ToInt32(dr["Inve1"]);
                    dinfo.Inve2 = HJConvert.ToString(dr["Inve2"]);



                    model.DeliveInfo = dinfo;

                    infos.Add(model);
                }
            }
            return infos;
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        /// <param name="orderid"></param>
        /// <param name="state">-1表示 状态没变</param>
        /// <param name="adminname"></param>
        /// <param name="Reve2"></param>
        /// <returns></returns>
        public int AddOrderRecord(string orderid, int state, string adminname, string Reve2)
        {
            SqlParameter[] Para =
            {
                new SqlParameter("@orderid",SqlDbType.VarChar,20),
                new SqlParameter("@newstate",SqlDbType.Int,4),
                new SqlParameter("@adminname",SqlDbType.VarChar,50),
                new SqlParameter("@Reve2",SqlDbType.VarChar,256),
            };
            Para[0].Value = orderid;
            Para[1].Value = state;
            Para[2].Value = adminname;
            Para[3].Value = Reve2;

            return SQLHelper.ExecuteNonQuery(CommandType.StoredProcedure, "Etogoorder_AddOrderRecord", Para);
        }

        /// <summary>
        /// 骑士客户端(Android)获取获取订单总数 2012.5.4 zjf@ihangjing.com 新增
        /// </summary>
        /// <param name="strWhere"></param>
        /// <returns></returns>
        public int DeliverGetOrderCount(string strWhere)
        {
            SqlParameter[] parameters =
            {
                new SqlParameter("@tblName" ,SqlDbType.VarChar , 300),
                new SqlParameter("@strWhere" , SqlDbType.VarChar  , 300)
            };
            parameters[0].Value = " Custorder LEFT JOIN dbo.OrderDeliver ON Custorder.OrderID=OrderDeliver.orderid ";
            parameters[1].Value = strWhere;

            return (int)SQLHelper.ExecuteScalar(CommandType.StoredProcedure, "pagecount_leftjoin", parameters);
        }


        /// <summary>
        /// 获取时间最早并且没有被打印的订单
        /// </summary>
        /// <param name="togoNum"></param>
        /// <returns></returns>
        public CustorderInfo GetOldOrder(string togoNum)
        {
            SqlParameter[] Para =
            {
                new SqlParameter("@TogoNum",SqlDbType.VarChar,20)
            };
            Para[0].Value = togoNum;

            CustorderInfo model = null;

            using (SqlDataReader dr = SQLHelper.ExecuteReader(CommandType.StoredProcedure, "ETogoOrder_GetOldOrder", Para))
            {
                if (dr.Read())
                {
                    model = new CustorderInfo();
                    model.Unid = HJConvert.ToInt32(dr["Unid"]);
                    model.InUse = HJConvert.ToString(dr["InUse"]);
                    model.OrderDateTime = HJConvert.ToDateTime(dr["OrderDateTime"]);
                    model.OrderChecker = HJConvert.ToInt32(dr["OrderChecker"]);
                    model.OrderStatus = HJConvert.ToInt32(dr["OrderStatus"]);
                    model.OrderRcver = HJConvert.ToString(dr["OrderRcver"]);
                    model.OrderComm = HJConvert.ToString(dr["OrderComm"]);
                    model.OrderAddress = HJConvert.ToString(dr["OrderAddress"]);
                    model.AddressText = HJConvert.ToString(dr["AddressText"]);
                    model.OrderAddrEx = HJConvert.ToString(dr["OrderAddrEx"]);
                    model.OrderAttach = HJConvert.ToString(dr["OrderAttach"]);
                    model.OrderSums = HJConvert.ToDecimal(dr["OrderSums"]);
                    model.Sender = HJConvert.ToString(dr["Sender"]);
                    model.SendTime = HJConvert.ToDateTime(dr["SendTime"]);
                    model.CallPhoneNo = HJConvert.ToString(dr["CallPhoneNo"]);
                    model.P2Sign = HJConvert.ToString(dr["P2Sign"]);
                    model.SendFee = HJConvert.ToDecimal(dr["SendFee"]);
                    model.paymode = HJConvert.ToInt32(dr["paymode"]);
                    model.paytime = HJConvert.ToDateTime(dr["paytime"]);
                    model.paymoney = HJConvert.ToDecimal(dr["paymoney"]);
                    model.paystate = HJConvert.ToInt32(dr["paystate"]);
                    model.SetStateTime = HJConvert.ToDateTime(dr["SetStateTime"]);
                    model.UserId = HJConvert.ToInt32(dr["UserId"]);
                    model.TogoId = HJConvert.ToInt32(dr["TogoId"]);
                    model.orderid = HJConvert.ToString(dr["orderid"]);
                    model.SystemUserId = HJConvert.ToInt32(dr["SystemUserId"]);
                    model.OldStatus = HJConvert.ToInt32(dr["OldStatus"]);
                    model.ReveVar1 = HJConvert.ToString(dr["ReveVar1"]);
                }
            }
            return model;
        }

        /// <summary>
        /// 修改订单的状态。
        /// </summary>
        /// <param name="OrderID">订单编号</param>
        /// <param name="State">要修改的订单状态</param>
        public int UpdataStatebyorderid(string OrderID, int State)
        {

            StringBuilder str = new StringBuilder();
            str.Append("update custorder set orderstatus=@State,SetStateTime='" + DateTime.Now + "' where OrderID=@OrderID");
            SqlParameter[] Para =
            {
                new SqlParameter("@OrderID",SqlDbType.VarChar,20),
                new SqlParameter("@State",SqlDbType.Int)
            };
            Para[0].Value = OrderID;
            Para[1].Value = State;

            return SQLHelper.ExecuteNonQuery(CommandType.Text, str.ToString(), Para);
        }

        /// <summary>
        /// 修改订单的状态。
        /// </summary>
        /// <param name="OrderID">订单编号</param>
        /// <param name="State">要修改的订单状态</param>
        public int UpdataStatebyorderidfix(string OrderID, int State)
        {

            StringBuilder str = new StringBuilder();

            str.Append("update custorder set sendstate=@State where OrderID=@OrderID");

            SqlParameter[] Para =
            {
                new SqlParameter("@OrderID",SqlDbType.VarChar,20),
                new SqlParameter("@State",SqlDbType.Int)
            };
            Para[0].Value = OrderID;
            Para[1].Value = State;

            return SQLHelper.ExecuteNonQuery(CommandType.Text, str.ToString(), Para);
        }

        /// <summary>
        /// 骑士客户端(Android)获取获取订单列表 2012.5.4 zjf@ihangjing.com 新增
        /// </summary>
        /// <param name="pagesize">页尺寸</param>
        /// <param name="pageindex">页索引</param>
        /// <param name="strWhere">查询条件</param>
        /// <param name="orderName">排序字段</param>
        /// <param name="orderType">排序类型，1为降序desc，0为升序asc</param>
        /// <returns>图书列表</returns>
        ///此代码由杭景科技代码内部生成器自动生成
        public IList<CustorderInfo> DeliverGetOrderList(int pagesize, int pageindex, string strWhere, string orderName, int orderType)
        {
            IList<CustorderInfo> infos = new List<CustorderInfo>();
            SqlParameter[] parameters =
            {
                new SqlParameter("@pagesize", SqlDbType.Int),
                new SqlParameter("@pageindex", SqlDbType.Int),
                new SqlParameter("@orderfield", SqlDbType.VarChar,50),
                new SqlParameter("@ordertype", SqlDbType.VarChar,5),
                new SqlParameter("@where", SqlDbType.VarChar,1500)
            };
            parameters[0].Value = pagesize;
            parameters[1].Value = pageindex;
            parameters[2].Value = orderName;
            if (orderType == 1)
            {
                parameters[3].Value = "desc";
            }
            else
            {
                parameters[3].Value = "asc";
            }
            parameters[4].Value = strWhere;

            using (SqlDataReader dr = SQLHelper.ExecuteReader(CommandType.StoredProcedure, "DeliverGetOrderListWithAll", parameters))
            {
                while (dr.Read())
                {
                    CustorderInfo info = new CustorderInfo();
                    info.Unid = HJConvert.ToInt32(dr["Unid"]);
                    info.UserId = HJConvert.ToInt32(dr["UserId"]);
                    info.TogoId = HJConvert.ToInt32(dr["TogoId"]);
                    info.orderid = HJConvert.ToString(dr["orderid"]);
                    info.OrderDateTime = HJConvert.ToDateTime(dr["OrderDateTime"]);
                    info.OrderAddress = HJConvert.ToString(dr["OrderAddress"]);



                    info.TogoName = HJConvert.ToString(dr["TogoName"]);
                    info.OrderRcver = HJConvert.ToString(dr["OrderRcver"]);
                    info.AddressText = HJConvert.ToString(dr["AddressText"]);
                    if (info.TogoId == 0)
                    {
                        info.TogoName = info.OrderRcver;
                    }


                    info.OrderAddrEx = HJConvert.ToString(dr["OrderAddrEx"]);
                    info.OrderAttach = HJConvert.ToString(dr["OrderAttach"]);
                    info.OrderSums = HJConvert.ToDecimal(dr["OrderSums"]);
                    info.DeliverGetSendFee = HJConvert.ToDecimal(dr["DeliverGetSendFee"]);
                    info.Sender = HJConvert.ToString(dr["Sender"]);

                    string isupdate = "0";

                    switch (info.Sender)
                    {
                        case "1":
                        case "10":
                            isupdate = info.Sender;
                            break;
                        default:
                            isupdate = "0";
                            break;
                    }
                    info.Sender = isupdate;



                    info.SendTime = HJConvert.ToDateTime(dr["SendTime"]);

                    info.OldStatus = HJConvert.ToInt32(dr["OldStatus"]);

                    info.OrderStatus = HJConvert.ToInt32(dr["OrderStatus"]);
                    info.paymode = HJConvert.ToInt32(dr["paymode"]);
                    info.paystate = HJConvert.ToInt32(dr["paystate"]);
                    info.SendFee = HJConvert.ToInt32(dr["SendFee"]);
                    info.sendstate = HJConvert.ToInt32(dr["sendstate"]);
                    info.IsShopSet = HJConvert.ToInt32(dr["IsShopSet"]);
                    info.SystemUserId = HJConvert.ToInt32(dr["SystemUserId"]);


                    infos.Add(info);
                }
            }
            return infos;
        }

        /// <summary>
        /// 骑士客户端通过获取列表的方式得到订单的model
        /// </summary>
        public IList<CustorderInfo> DeliverGetOrderList4GetModel(string orderid)
        {
            IList<CustorderInfo> infos = new List<CustorderInfo>();
            SqlParameter[] parameters =
            {
                new SqlParameter("@orderid", SqlDbType.VarChar,255),
            };
            parameters[0].Value = orderid;

            using (SqlDataReader dr = SQLHelper.ExecuteReader(CommandType.StoredProcedure, "Custorder_delivergetModel", parameters))
            {
                while (dr.Read())
                {
                    CustorderInfo model = new CustorderInfo();

                    model.Unid = HJConvert.ToInt32(dr["Unid"]);
                    model.InUse = HJConvert.ToString(dr["InUse"]);
                    model.OrderDateTime = HJConvert.ToDateTime(dr["OrderDateTime"]);
                    model.OrderChecker = HJConvert.ToInt32(dr["OrderChecker"]);
                    model.OrderStatus = HJConvert.ToInt32(dr["OrderStatus"]);
                    model.OrderRcver = HJConvert.ToString(dr["OrderRcver"]);
                    model.OrderComm = HJConvert.ToString(dr["OrderComm"]);
                    model.OrderAddress = HJConvert.ToString(dr["OrderAddress"]);
                    model.AddressText = HJConvert.ToString(dr["AddressText"]);
                    model.OrderAddrEx = HJConvert.ToString(dr["OrderAddrEx"]);
                    model.OrderAttach = HJConvert.ToString(dr["OrderAttach"]);
                    model.shopauthuser = HJConvert.ToString(dr["shopauthuser"]);
                    model.OrderSums = HJConvert.ToDecimal(dr["OrderSums"]);
                    model.Sender = HJConvert.ToString(dr["Sender"]);
                    model.SendTime = HJConvert.ToDateTime(dr["SendTime"]);
                    model.CallPhoneNo = HJConvert.ToString(dr["CallPhoneNo"]);
                    model.P2Sign = HJConvert.ToString(dr["P2Sign"]);
                    model.SendFee = HJConvert.ToDecimal(dr["SendFee"]);
                    model.paymode = HJConvert.ToInt32(dr["paymode"]);
                    model.paytime = HJConvert.ToDateTime(dr["paytime"]);
                    model.paymoney = HJConvert.ToDecimal(dr["paymoney"]);
                    model.paystate = HJConvert.ToInt32(dr["paystate"]);
                    model.SetStateTime = HJConvert.ToDateTime(dr["SetStateTime"]);
                    model.UserId = HJConvert.ToInt32(dr["UserId"]);
                    model.TogoId = HJConvert.ToInt32(dr["TogoId"]);
                    model.orderid = HJConvert.ToString(dr["orderid"]);
                    model.SystemUserId = HJConvert.ToInt32(dr["SystemUserId"]);
                    model.OldStatus = HJConvert.ToInt32(dr["OldStatus"]);
                    model.TogoTel = HJConvert.ToString(dr["togotel"]);
                    model.TogoName = HJConvert.ToString(dr["togoname"]);
                    model.TogoAddress = HJConvert.ToString(dr["togoaddress"]);
                    model.sendstate = HJConvert.ToInt32(dr["sendstate"]);
                    model.OldPrice = HJConvert.ToDecimal(dr["OldPrice"]);
                    model.ReveVar2 = HJConvert.ToString(dr["ReveVar2"]);
                    model.IsShopSet = HJConvert.ToInt32(dr["IsShopSet"]);
                    model.shopdiscountmoney = HJConvert.ToDecimal(dr["shopdiscountmoney"]);

                    model.DeliverGetSendFee = HJConvert.ToDecimal(dr["DeliverGetSendFee"]);
                    model.delivergetfee_base = HJConvert.ToDecimal(dr["delivergetfee_base"]);


                    model.cardids = HJConvert.ToString(dr["cardids"]);
                    model.Commission = HJConvert.ToDecimal(dr["Commission"]);
                    model.settlecycle = HJConvert.ToInt32(dr["settlecycle"]);
                    model.Advance = HJConvert.ToDecimal(dr["Advance"]);
                    model.Mergeorderremark = HJConvert.ToString(dr["Mergeorderremark"]);


                    infos.Add(model);
                }
            }
            return infos;
        }

        /// <summary>
        /// 获取配送员最后接单的且在派送的订单
        /// </summary>
        ///此代码由杭景科技代码内部生成器自动生成
        public IList<simpleorderInfo> GetDliverList(string did)
        {
            IList<simpleorderInfo> infos = new List<simpleorderInfo>();
            string sql = " SELECT TOP 10 a.OrderRcver,a.AddressText,a.orderid,a.OrderComm ,b.Name,b.Address AS togoadddress,ReveVar2 from Custorder a left join points b on a.togoid = b.unid LEFT JOIN dbo.OrderDeliver ON dbo.OrderDeliver.OrderId = a.orderid where a.deliverid =" + did + " and a.delflag = 0 and OrderStatus = 7 and sendstate between 1 and 2 ORDER BY DeliveryTime DESC";

            using (SqlDataReader dr = SQLHelper.ExecuteReader(CommandType.Text, sql, null))
            {
                while (dr.Read())
                {
                    simpleorderInfo order = new simpleorderInfo();
                    order.TogoAddress = HJConvert.ToString(dr["togoadddress"]);
                    order.AddressText = HJConvert.ToString(dr["AddressText"]);
                    order.TogoName = HJConvert.ToString(dr["Name"]);
                    order.OrderID = HJConvert.ToString(dr["OrderID"]);
                    order.latlng = HJConvert.ToString(dr["ReveVar2"]);
                    infos.Add(order);
                }
            }
            return infos;
        }
        /// <summary>
        /// 获得所有状态为新增的订单
        /// </summary>
        /// <returns></returns>
        public IList<CustorderInfo> GetNewOrder()
        {
            IList<CustorderInfo> DataList = new List<CustorderInfo>();

            using (SqlDataReader dr = SQLHelper.ExecuteReader(CommandType.StoredProcedure, "ETogoOrder_GetNewOrder", null))
            {
                while (dr.Read())
                {
                    CustorderInfo model = new CustorderInfo();
                    model.orderid = HJConvert.ToString(dr["OrderID"]);
                    model.OrderStatus = HJConvert.ToInt32(dr["OrderStatus"]);
                    model.OrderDateTime = HJConvert.ToDateTime(dr["OrderDateTime"]);
                    DataList.Add(model);
                }
            }
            return DataList;
        }

        /// <summary>
        /// 要推给商家的订单数量
        /// </summary>
        public int SendShopOrderCount(string shopid)
        {
            return Convert.ToInt32(SQLHelper.ExecuteScalar(CommandType.StoredProcedure, "SendShopOrderCount", new SqlParameter("@shopid", shopid)));
        }

        /// <summary>
        /// 要推给骑士的订单数量
        /// </summary>
        public OrderCountInfo SendDeliverOrderCount(string deliverid)
        {
            OrderCountInfo model = new OrderCountInfo();
            model.rat = 0;
            model.CountIntValue = 0;


            using (SqlDataReader dr = SQLHelper.ExecuteReader(CommandType.StoredProcedure, "SendDeliverOrderCount", new SqlParameter("@dataid", deliverid)))
            {
                while (dr.Read())
                {
                    model.CountIntValue = HJConvert.ToInt32(dr["ordercount"]);

                }
            }

            return model;
        }

        /// <summary>
        /// 配送完成相关操作
        /// </summary>
        /// <param name="orderid"></param>
        /// <param name="did"></param>
        /// <returns></returns>
        public int deliverCompleteX(string orderid, int did)
        {
            AddOrderRecord(orderid, 3, "骑士", "骑士修改订单状态 SaveOrderState.aspx");
            AddPoint(orderid);

            StringBuilder str = new StringBuilder();

            str.Append("UPDATE dbo.Custorder SET ReveInt1 = datediff( MINUTE, SendTime, GETDATE()) WHERE orderid = @orderid;");
            str.Append("UPDATE dbo.Custorder SET deliversiteid = 10  WHERE orderid = @orderid AND  ReveInt1 > 60;");

            str.Append("update OrderDeliver set OverTime=getdate() where OrderID=@OrderID");

            SqlParameter[] Para =
            {
                new SqlParameter("@OrderID",SqlDbType.VarChar,20),
            };
            Para[0].Value = orderid;

            SQLHelper.ExecuteNonQuery(CommandType.Text, str.ToString(), Para);



            return 1;
        }

        /// <summary>
        /// 营业收入订单总数，和金额
        /// </summary>
        /// <param name="togoNum"></param>
        /// <param name="time1"></param>
        /// <param name="time2"></param>
        /// <returns></returns>
        public CustorderInfo SiteIncomeStatistics(string where)
        {
            string sql = "SELECT COUNT(1) AS	OrderCount , SUM(shopdiscountmoney) as shopdiscountmoney, SUM(OrderSums) as CountDecimalPrice FROM dbo.Custorder   where " + where;

            CustorderInfo model = new CustorderInfo();
            using (SqlDataReader dr = SQLHelper.ExecuteReader(CommandType.Text, sql, null))
            {
                if (dr.Read())
                {

                    model.OrderCount = HJConvert.ToInt32(dr["OrderCount"]);
                    model.OrderSums = HJConvert.ToDecimal(dr["CountDecimalPrice"]);//代收金额
                    model.shopdiscountmoney = HJConvert.ToDecimal(dr["shopdiscountmoney"]);//配送费
                }
            }
            return model;
        }

        /// <summary>
        /// 订单来源分布
        /// </summary>
        /// <param name="where"></param>
        /// <returns></returns>
        public IList<OrderCountInfo> GetOrderByOrderSource(string where)
        {
            IList<OrderCountInfo> list = new List<OrderCountInfo>();
            string strSql = " SELECT fromweb ,COUNT(1) AS ordercount , SUM(OrderSums) AS TotalPrice FROM dbo.Custorder where  " + where + "    group by fromweb";

            OrderCountInfo info = new OrderCountInfo();

            using (SqlDataReader dr = SQLHelper.ExecuteReader(CommandType.Text, strSql, null))
            {
                while (dr.Read())
                {
                    info = new OrderCountInfo();

                    info.CountKey = HJConvert.ToString(dr["fromweb"]);
                    info.CountIntValue = HJConvert.ToInt32(dr["ordercount"]);
                    info.CountDecimalValue = HJConvert.ToDecimal(dr["TotalPrice"]);
                    list.Add(info);
                }
            }

            return list;
        }

        /// <summary>
        /// 订单状态分布
        /// </summary>
        /// <param name="where"></param>
        /// <returns></returns>
        public IList<OrderCountInfo> GetOrderByOrderState(string where)
        {
            IList<OrderCountInfo> list = new List<OrderCountInfo>();
            string strSql = " SELECT OrderStatus ,COUNT(1) AS ordercount , SUM(OrderSums) AS TotalPrice FROM dbo.Custorder where  " + where + "    group by OrderStatus";

            OrderCountInfo info = new OrderCountInfo();

            using (SqlDataReader dr = SQLHelper.ExecuteReader(CommandType.Text, strSql, null))
            {
                while (dr.Read())
                {
                    info = new OrderCountInfo();

                    info.CountKey = HJConvert.ToString(dr["OrderStatus"]);
                    info.CountIntValue = HJConvert.ToInt32(dr["ordercount"]);
                    info.CountDecimalValue = HJConvert.ToDecimal(dr["TotalPrice"]);
                    list.Add(info);
                }
            }

            return list;
        }
    }
}
